From 3746723f526e43b2829aa6b3599c81d2e8653542 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Wed, 5 May 2021 03:24:08 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8FAdd=20type=20to=20MIN-files?= =?UTF-8?q?=20and=20TRN-files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/diablo.cpp | 16 ++++++++-------- Source/engine.cpp | 5 +++-- Source/engine.h | 10 ++++++++-- Source/gendung.cpp | 35 ++++++++++++++--------------------- Source/gendung.h | 2 +- Source/lighting.cpp | 20 ++++++-------------- Source/monster.cpp | 37 ++++++++++++++++--------------------- Source/monster.h | 5 ----- Source/objects.cpp | 34 ++++++++++++---------------------- 9 files changed, 68 insertions(+), 96 deletions(-) diff --git a/Source/diablo.cpp b/Source/diablo.cpp index f129279a0..69ac0ae8e 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -1505,11 +1505,11 @@ void LoadLvlGFX() if (gbIsHellfire) { pDungeonCels = LoadFileInMem("NLevels\\TownData\\Town.CEL"); pMegaTiles = LoadFileInMem("NLevels\\TownData\\Town.TIL"); - pLevelPieces = LoadFileInMem("NLevels\\TownData\\Town.MIN"); + pLevelPieces = LoadFileInMem("NLevels\\TownData\\Town.MIN"); } else { pDungeonCels = LoadFileInMem("Levels\\TownData\\Town.CEL"); pMegaTiles = LoadFileInMem("Levels\\TownData\\Town.TIL"); - pLevelPieces = LoadFileInMem("Levels\\TownData\\Town.MIN"); + pLevelPieces = LoadFileInMem("Levels\\TownData\\Town.MIN"); } pSpecialCels = LoadCel("Levels\\TownData\\TownS.CEL", SpecialCelWidth); break; @@ -1517,37 +1517,37 @@ void LoadLvlGFX() if (currlevel < 21) { pDungeonCels = LoadFileInMem("Levels\\L1Data\\L1.CEL"); pMegaTiles = LoadFileInMem("Levels\\L1Data\\L1.TIL"); - pLevelPieces = LoadFileInMem("Levels\\L1Data\\L1.MIN"); + pLevelPieces = LoadFileInMem("Levels\\L1Data\\L1.MIN"); pSpecialCels = LoadCel("Levels\\L1Data\\L1S.CEL", SpecialCelWidth); } else { pDungeonCels = LoadFileInMem("NLevels\\L5Data\\L5.CEL"); pMegaTiles = LoadFileInMem("NLevels\\L5Data\\L5.TIL"); - pLevelPieces = LoadFileInMem("NLevels\\L5Data\\L5.MIN"); + pLevelPieces = LoadFileInMem("NLevels\\L5Data\\L5.MIN"); pSpecialCels = LoadCel("NLevels\\L5Data\\L5S.CEL", SpecialCelWidth); } break; case DTYPE_CATACOMBS: pDungeonCels = LoadFileInMem("Levels\\L2Data\\L2.CEL"); pMegaTiles = LoadFileInMem("Levels\\L2Data\\L2.TIL"); - pLevelPieces = LoadFileInMem("Levels\\L2Data\\L2.MIN"); + pLevelPieces = LoadFileInMem("Levels\\L2Data\\L2.MIN"); pSpecialCels = LoadCel("Levels\\L2Data\\L2S.CEL", SpecialCelWidth); break; case DTYPE_CAVES: if (currlevel < 17) { pDungeonCels = LoadFileInMem("Levels\\L3Data\\L3.CEL"); pMegaTiles = LoadFileInMem("Levels\\L3Data\\L3.TIL"); - pLevelPieces = LoadFileInMem("Levels\\L3Data\\L3.MIN"); + pLevelPieces = LoadFileInMem("Levels\\L3Data\\L3.MIN"); } else { pDungeonCels = LoadFileInMem("NLevels\\L6Data\\L6.CEL"); pMegaTiles = LoadFileInMem("NLevels\\L6Data\\L6.TIL"); - pLevelPieces = LoadFileInMem("NLevels\\L6Data\\L6.MIN"); + pLevelPieces = LoadFileInMem("NLevels\\L6Data\\L6.MIN"); } pSpecialCels = LoadCel("Levels\\L1Data\\L1S.CEL", SpecialCelWidth); break; case DTYPE_HELL: pDungeonCels = LoadFileInMem("Levels\\L4Data\\L4.CEL"); pMegaTiles = LoadFileInMem("Levels\\L4Data\\L4.TIL"); - pLevelPieces = LoadFileInMem("Levels\\L4Data\\L4.MIN"); + pLevelPieces = LoadFileInMem("Levels\\L4Data\\L4.MIN"); pSpecialCels = LoadCel("Levels\\L2Data\\L2S.CEL", SpecialCelWidth); break; default: diff --git a/Source/engine.cpp b/Source/engine.cpp index 3c09d4c17..22045f015 100644 --- a/Source/engine.cpp +++ b/Source/engine.cpp @@ -11,6 +11,8 @@ * - Video playback */ +#include + #include "lighting.h" #include "movie.h" #include "options.h" @@ -765,10 +767,9 @@ DWORD LoadFileWithMem(const char *pszName, BYTE *p) * @param ttbl Palette translation table * @param nCel Frame number in CL2 file */ -void Cl2ApplyTrans(BYTE *p, BYTE *ttbl, int nCel) +void Cl2ApplyTrans(BYTE *p, const std::array &ttbl, int nCel) { assert(p != nullptr); - assert(ttbl != nullptr); for (int i = 1; i <= nCel; i++) { int nDataSize; diff --git a/Source/engine.h b/Source/engine.h index fb25ba6a4..8372d8f94 100644 --- a/Source/engine.h +++ b/Source/engine.h @@ -609,10 +609,16 @@ int32_t GenerateRnd(int32_t v); size_t GetFileSize(const char *pszName); void LoadFileData(const char *pszName, byte *buffer, size_t bufferSize); +template +void LoadFileInMem(const char *path, T *data, std::size_t count) +{ + LoadFileData(path, reinterpret_cast(data), count * sizeof(T)); +} + template void LoadFileInMem(const char *path, std::array &data) { - LoadFileData(path, reinterpret_cast(&data), N * sizeof(T)); + LoadFileInMem(path, &data, N); } /** @@ -640,7 +646,7 @@ std::unique_ptr LoadFileInMem(const char *path, size_t *elements = nullptr) } DWORD LoadFileWithMem(const char *pszName, BYTE *p); -void Cl2ApplyTrans(BYTE *p, BYTE *ttbl, int nCel); +void Cl2ApplyTrans(BYTE *p, const std::array &ttbl, int nCel); void PlayInGameMovie(const char *pszMovie); } // namespace devilution diff --git a/Source/gendung.cpp b/Source/gendung.cpp index 227992e1f..b37986908 100644 --- a/Source/gendung.cpp +++ b/Source/gendung.cpp @@ -29,7 +29,7 @@ bool setloadflag; std::optional pSpecialCels; /** Specifies the tile definitions of the active dungeon type; (e.g. levels/l1data/l1.til). */ std::unique_ptr pMegaTiles; -std::unique_ptr pLevelPieces; +std::unique_ptr pLevelPieces; std::unique_ptr pDungeonCels; std::array block_lvid; std::array nBlockTable; @@ -145,43 +145,36 @@ void FillSolidBlockTbls() nBlockTable[i + 1] = (bv & 0x02) != 0; nMissileTable[i + 1] = (bv & 0x04) != 0; nTransTable[i + 1] = (bv & 0x08) != 0; - nTrapTable[i + 1] = (bv & 0x80) != 0 ; + nTrapTable[i + 1] = (bv & 0x80) != 0; block_lvid[i + 1] = (bv & 0x70) >> 4; } } void SetDungeonMicros() { - int i, x, y, lv, blocks; - uint16_t *pPiece; - MICROS *pMap; + MicroTileLen = 10; + int blocks = 10; if (leveltype == DTYPE_TOWN) { MicroTileLen = 16; blocks = 16; - } else if (leveltype != DTYPE_HELL) { - MicroTileLen = 10; - blocks = 10; - } else { + } else if (leveltype == DTYPE_HELL) { MicroTileLen = 12; blocks = 16; } - for (y = 0; y < MAXDUNY; y++) { - for (x = 0; x < MAXDUNX; x++) { - lv = dPiece[x][y]; - pMap = &dpiece_defs_map_2[x][y]; + for (int y = 0; y < MAXDUNY; y++) { + for (int x = 0; x < MAXDUNX; x++) { + int lv = dPiece[x][y]; + MICROS µs = dpiece_defs_map_2[x][y]; if (lv != 0) { lv--; - if (leveltype != DTYPE_HELL && leveltype != DTYPE_TOWN) - pPiece = (uint16_t *)&pLevelPieces[20 * lv]; - else - pPiece = (uint16_t *)&pLevelPieces[32 * lv]; - for (i = 0; i < blocks; i++) - pMap->mt[i] = SDL_SwapLE16(pPiece[(i & 1) + blocks - 2 - (i & 0xE)]); + uint16_t *pieces = &pLevelPieces[blocks * lv]; + for (int i = 0; i < blocks; i++) + micros.mt[i] = SDL_SwapLE16(pieces[blocks - 2 + (i & 1) - (i & 0xE)]); } else { - for (i = 0; i < blocks; i++) - pMap->mt[i] = 0; + for (int i = 0; i < blocks; i++) + micros.mt[i] = 0; } } } diff --git a/Source/gendung.h b/Source/gendung.h index bb8e1d907..24e64ee24 100644 --- a/Source/gendung.h +++ b/Source/gendung.h @@ -131,7 +131,7 @@ extern std::unique_ptr pSetPiece; extern bool setloadflag; extern std::optional pSpecialCels; extern std::unique_ptr pMegaTiles; -extern std::unique_ptr pLevelPieces; +extern std::unique_ptr pLevelPieces; extern std::unique_ptr pDungeonCels; /** * List of transparancy masks to use for dPieces diff --git a/Source/lighting.cpp b/Source/lighting.cpp index 2bcd75c0f..352a62527 100644 --- a/Source/lighting.cpp +++ b/Source/lighting.cpp @@ -22,7 +22,7 @@ char lightmax; bool dolighting; BYTE lightblock[64][16][16]; int visionid; -std::array pLightTbl; +std::array pLightTbl; bool lightflag; /** @@ -769,7 +769,7 @@ void MakeLightTable() int i, j, k, l, lights, shade, l1, l2, cnt, rem, div; double fs, fa; BYTE col, max; - BYTE *tbl; + uint8_t *tbl; BYTE blood[16]; tbl = pLightTbl.data(); @@ -893,19 +893,11 @@ void MakeLightTable() tbl += 240; } - { - auto trn = LoadFileInMem("PlrGFX\\Infra.TRN"); - for (i = 0; i < 256; i++) { - *tbl++ = trn[i]; - } - } + LoadFileInMem("PlrGFX\\Infra.TRN", tbl, 256); + tbl += 256; - { - auto trn = LoadFileInMem("PlrGFX\\Stone.TRN"); - for (i = 0; i < 256; i++) { - *tbl++ = trn[i]; - } - } + LoadFileInMem("PlrGFX\\Stone.TRN", tbl, 256); + tbl += 256; for (i = 0; i < 8; i++) { for (col = 226; col < 239; col++) { diff --git a/Source/monster.cpp b/Source/monster.cpp index 2ecfee205..279173f8c 100644 --- a/Source/monster.cpp +++ b/Source/monster.cpp @@ -6,6 +6,7 @@ #include "monster.h" #include +#include #include #include "control.h" @@ -141,28 +142,24 @@ void (*AiProc[])(int i) = { &MAI_BoneDemon }; -void InitMonsterTRN(int monst, bool special) +void InitMonsterTRN(CMonster &monst) { - BYTE *f; - int i, n, j; + std::array colorTranslations; + LoadFileInMem(monst.MData->TransFile, colorTranslations); - f = Monsters[monst].trans_file; - for (i = 0; i < 256; i++) { - if (*f == 255) { - *f = 0; + std::replace(colorTranslations.begin(), colorTranslations.end(), 255, 0); + + int n = monst.MData->has_special ? 6 : 5; + for (int i = 0; i < n; i++) { + if (i == 1 && monst.mtype >= MT_COUNSLR && monst.mtype <= MT_ADVOCATE) { + continue; } - f++; - } - n = special ? 6 : 5; - for (i = 0; i < n; i++) { - if (i != 1 || Monsters[monst].mtype < MT_COUNSLR || Monsters[monst].mtype > MT_ADVOCATE) { - for (j = 0; j < 8; j++) { - Cl2ApplyTrans( - Monsters[monst].Anims[i].Data[j], - Monsters[monst].trans_file, - Monsters[monst].Anims[i].Frames); - } + for (int j = 0; j < 8; j++) { + Cl2ApplyTrans( + monst.Anims[i].Data[j], + colorTranslations, + monst.Anims[i].Frames); } } } @@ -384,9 +381,7 @@ void InitMonsterGFX(int monst) Monsters[monst].MData = &monsterdata[mtype]; if (monsterdata[mtype].has_trans) { - Monsters[monst].trans_file = LoadFileInMem(monsterdata[mtype].TransFile).release(); - InitMonsterTRN(monst, monsterdata[mtype].has_special); - delete[] Monsters[monst].trans_file; + InitMonsterTRN(Monsters[monst]); } if (mtype >= MT_NMAGMA && mtype <= MT_WMAGMA && !(MissileFileFlag & 1)) { diff --git a/Source/monster.h b/Source/monster.h index 400285840..7165d4ed5 100644 --- a/Source/monster.h +++ b/Source/monster.h @@ -130,11 +130,6 @@ struct CMonster { uint8_t mAFNum; int8_t mdeadval; const MonsterData *MData; - /** - * A TRN file contains a sequence of color transitions, represented - * as indexes into a palette. (a 256 byte array of palette indices) - */ - uint8_t *trans_file; }; struct MonsterStruct { // note: missing field _mAFNum diff --git a/Source/objects.cpp b/Source/objects.cpp index 60ac0687f..1035e4f10 100644 --- a/Source/objects.cpp +++ b/Source/objects.cpp @@ -2237,37 +2237,27 @@ void ProcessObjects() void ObjSetMicro(int dx, int dy, int pn) { - uint16_t *v; - MICROS *defs; - int i; - dPiece[dx][dy] = pn; pn--; - defs = &dpiece_defs_map_2[dx][dy]; - if (leveltype != DTYPE_HELL) { - v = (uint16_t *)pLevelPieces.get() + 10 * pn; - for (i = 0; i < 10; i++) { - defs->mt[i] = SDL_SwapLE16(v[(i & 1) - (i & 0xE) + 8]); - } - } else { - v = (uint16_t *)pLevelPieces.get() + 16 * pn; - for (i = 0; i < 16; i++) { - defs->mt[i] = SDL_SwapLE16(v[(i & 1) - (i & 0xE) + 14]); - } + + int blocks = leveltype != DTYPE_HELL ? 10 : 16; + + uint16_t *piece = &pLevelPieces[blocks * pn ]; + MICROS µs = dpiece_defs_map_2[dx][dy]; + + for (int i = 0; i < blocks; i++) { + micros.mt[i] = SDL_SwapLE16(piece[blocks - 2 + (i & 1) - (i & 0xE)]); } } void objects_set_door_piece(int x, int y) { - int pn; - long v1, v2; + int pn = dPiece[x][y] - 1; - pn = dPiece[x][y] - 1; + uint16_t *piece = &pLevelPieces[10 * pn + 8]; - v1 = *((uint16_t *)pLevelPieces.get() + 10 * pn + 8); - v2 = *((uint16_t *)pLevelPieces.get() + 10 * pn + 9); - dpiece_defs_map_2[x][y].mt[0] = SDL_SwapLE16(v1); - dpiece_defs_map_2[x][y].mt[1] = SDL_SwapLE16(v2); + dpiece_defs_map_2[x][y].mt[0] = SDL_SwapLE16(piece[0]); + dpiece_defs_map_2[x][y].mt[1] = SDL_SwapLE16(piece[1]); } void ObjSetMini(int x, int y, int v)