From 12d4b61b684b19686be5f3d9fe2e279a52367529 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Tue, 3 Nov 2020 00:58:45 +0100 Subject: [PATCH 01/12] Fix compiler warrnings related to clicktype --- enums.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/enums.h b/enums.h index ca0e4a052..04dbbc36e 100644 --- a/enums.h +++ b/enums.h @@ -2295,7 +2295,7 @@ typedef enum clicktype { CLICK_NONE = 0, CLICK_LEFT = 1, CLICK_RIGHT = 2, -}; +} clicktype; /* First 5 bits store level From 17d70648f5f2ccc15bfb0befab899e987d62d255 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Tue, 3 Nov 2020 01:28:31 +0100 Subject: [PATCH 02/12] Clean up inv.h --- Source/inv.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Source/inv.h b/Source/inv.h index 4bdd578ca..f25f4ac31 100644 --- a/Source/inv.h +++ b/Source/inv.h @@ -11,17 +11,13 @@ extern BOOL drawsbarflag; void FreeInvGFX(); void InitInv(); -void InvDrawSlotBack(int X, int Y, int W, int H); void DrawInv(); void DrawInvBelt(); BOOL AutoPlace(int pnum, int ii, int sx, int sy, BOOL saveflag); BOOL SpecialAutoPlace(int pnum, int ii, int sx, int sy, BOOL saveflag); BOOL GoldAutoPlace(int pnum); -BOOL WeaponAutoPlace(int pnum); int SwapItem(ItemStruct *a, ItemStruct *b); -void CheckInvPaste(int pnum, int mx, int my); void CheckInvSwap(int pnum, BYTE bLoc, int idx, WORD wCI, int seed, BOOL bId); -void CheckInvCut(int pnum, int mx, int my); void inv_update_rem_item(int pnum, BYTE iv); void RemoveInvItem(int pnum, int iv); #ifdef HELLFIRE @@ -31,8 +27,6 @@ void RemoveSpdBarItem(int pnum, int iv); void CheckInvItem(); void CheckInvScrn(); void CheckItemStats(int pnum); -void CheckBookLevel(int pnum); -void CheckQuestItem(int pnum); void InvGetItem(int pnum, int ii); void AutoGetItem(int pnum, int ii); int FindGetItem(int idx, WORD ci, int iseed); @@ -51,7 +45,6 @@ void RemoveScroll(int pnum); BOOL UseScroll(); void UseStaffCharge(int pnum); BOOL UseStaff(); -void StartGoldDrop(); BOOL UseInvItem(int pnum, int cii); void DoTelekinesis(); int CalculateGold(int pnum); From 9f9a7920d4e3a245d65995e2a95cb3f39cf525aa Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Tue, 3 Nov 2020 02:05:01 +0100 Subject: [PATCH 03/12] Reorder items.cpp --- Source/items.cpp | 205 ++++++++++++++++++++++++----------------------- Source/items.h | 74 ----------------- 2 files changed, 103 insertions(+), 176 deletions(-) diff --git a/Source/items.cpp b/Source/items.cpp index 1f30aff9b..96fc642fa 100644 --- a/Source/items.cpp +++ b/Source/items.cpp @@ -492,31 +492,6 @@ int items_get_currlevel() return lvl; } - -void items_42390F() -{ - int x, y, id; - - x = random_(12, 80) + 16; - y = random_(12, 80) + 16; - while (!ItemPlace(x, y)) { - x = random_(12, 80) + 16; - y = random_(12, 80) + 16; - } - switch (currlevel) { - case 22: - id = IDI_NOTE2; - break; - case 23: - id = IDI_NOTE3; - break; - default: - id = IDI_NOTE1; - break; - } - SpawnQuestItem(id, x, y, 0, 1); -} - #endif void InitItemGFX() @@ -598,6 +573,32 @@ void AddInitItems() } } +#ifdef HELLFIRE +static void items_42390F() +{ + int x, y, id; + + x = random_(12, 80) + 16; + y = random_(12, 80) + 16; + while (!ItemPlace(x, y)) { + x = random_(12, 80) + 16; + y = random_(12, 80) + 16; + } + switch (currlevel) { + case 22: + id = IDI_NOTE2; + break; + case 23: + id = IDI_NOTE3; + break; + default: + id = IDI_NOTE1; + break; + } + SpawnQuestItem(id, x, y, 0, 1); +} +#endif + void InitItems() { int i; @@ -1210,6 +1211,20 @@ void CalcSelfItems(int pnum) } while (changeflag); } +static BOOL ItemMinStats(PlayerStruct *p, ItemStruct *x) +{ + if (p->_pMagic < x->_iMinMag) + return FALSE; + + if (p->_pStrength < x->_iMinStr) + return FALSE; + + if (p->_pDexterity < x->_iMinDex) + return FALSE; + + return TRUE; +} + void CalcPlrItemMin(int pnum) { PlayerStruct *p; @@ -1234,20 +1249,6 @@ void CalcPlrItemMin(int pnum) } } -BOOL ItemMinStats(PlayerStruct *p, ItemStruct *x) -{ - if (p->_pMagic < x->_iMinMag) - return FALSE; - - if (p->_pStrength < x->_iMinStr) - return FALSE; - - if (p->_pDexterity < x->_iMinDex) - return FALSE; - - return TRUE; -} - void CalcPlrBookVals(int p) { int i, slvl; @@ -3516,28 +3517,7 @@ void CheckIdentify(int pnum, int cii) SetCursor_(CURSOR_HAND); } -void DoRepair(int pnum, int cii) -{ - PlayerStruct *p; - ItemStruct *pi; - - p = &plr[pnum]; - PlaySfxLoc(IS_REPAIR, p->_px, p->_py); - - if (cii >= NUM_INVLOC) { - pi = &p->InvList[cii - NUM_INVLOC]; - } else { - pi = &p->InvBody[cii]; - } - - RepairItem(pi, p->_pLevel); - CalcPlrInv(pnum, TRUE); - - if (pnum == myplr) - SetCursor_(CURSOR_HAND); -} - -void RepairItem(ItemStruct *i, int lvl) +static void RepairItem(ItemStruct *i, int lvl) { int rep, d; @@ -3568,6 +3548,42 @@ void RepairItem(ItemStruct *i, int lvl) i->_iDurability = i->_iMaxDur; } +void DoRepair(int pnum, int cii) +{ + PlayerStruct *p; + ItemStruct *pi; + + p = &plr[pnum]; + PlaySfxLoc(IS_REPAIR, p->_px, p->_py); + + if (cii >= NUM_INVLOC) { + pi = &p->InvList[cii - NUM_INVLOC]; + } else { + pi = &p->InvBody[cii]; + } + + RepairItem(pi, p->_pLevel); + CalcPlrInv(pnum, TRUE); + + if (pnum == myplr) + SetCursor_(CURSOR_HAND); +} + +static void RechargeItem(ItemStruct *i, int r) +{ + if (i->_iCharges != i->_iMaxCharges) { + do { + i->_iMaxCharges--; + if (i->_iMaxCharges == 0) { + return; + } + i->_iCharges += r; + } while (i->_iCharges < i->_iMaxCharges); + if (i->_iCharges > i->_iMaxCharges) + i->_iCharges = i->_iMaxCharges; + } +} + void DoRecharge(int pnum, int cii) { PlayerStruct *p; @@ -3592,21 +3608,7 @@ void DoRecharge(int pnum, int cii) } #ifdef HELLFIRE -void DoOil(int pnum, int cii) -{ - PlayerStruct *p = &plr[pnum]; - - if (cii >= NUM_INVLOC || cii == INVLOC_HEAD || (cii > INVLOC_AMULET && cii <= INVLOC_CHEST)) { - if (OilItem(&p->InvBody[cii], p)) { - CalcPlrInv(pnum, TRUE); - if (pnum == myplr) { - SetCursor_(CURSOR_HAND); - } - } - } -} - -BOOL OilItem(ItemStruct *x, PlayerStruct *p) +static BOOL OilItem(ItemStruct *x, PlayerStruct *p) { int dur, r; @@ -3726,21 +3728,20 @@ BOOL OilItem(ItemStruct *x, PlayerStruct *p) return TRUE; } -#endif -void RechargeItem(ItemStruct *i, int r) +void DoOil(int pnum, int cii) { - if (i->_iCharges != i->_iMaxCharges) { - do { - i->_iMaxCharges--; - if (i->_iMaxCharges == 0) { - return; + PlayerStruct *p = &plr[pnum]; + + if (cii >= NUM_INVLOC || cii == INVLOC_HEAD || (cii > INVLOC_AMULET && cii <= INVLOC_CHEST)) { + if (OilItem(&p->InvBody[cii], p)) { + CalcPlrInv(pnum, TRUE); + if (pnum == myplr) { + SetCursor_(CURSOR_HAND); } - i->_iCharges += r; - } while (i->_iCharges < i->_iMaxCharges); - if (i->_iCharges > i->_iMaxCharges) - i->_iCharges = i->_iMaxCharges; + } } } +#endif void PrintItemOil(char IDidx) { @@ -4928,7 +4929,7 @@ int RndPremiumItem(int minlvl, int maxlvl) return ril[random_(50, ri)] + 1; } -void SpawnOnePremium(int i, int plvl) +static void SpawnOnePremium(int i, int plvl) { int itype; ItemStruct holditem; @@ -5661,6 +5662,20 @@ void CreateMagicWeapon(int x, int y, int imisc, int icurs, BOOL sendmsg, BOOL de } } +static void NextItemRecord(int i) +{ + gnNumGetRecords--; + + if (gnNumGetRecords == 0) { + return; + } + + itemrecord[i].dwTimestamp = itemrecord[gnNumGetRecords].dwTimestamp; + itemrecord[i].nSeed = itemrecord[gnNumGetRecords].nSeed; + itemrecord[i].wCI = itemrecord[gnNumGetRecords].wCI; + itemrecord[i].nIndex = itemrecord[gnNumGetRecords].nIndex; +} + BOOL GetItemRecord(int nSeed, WORD wCI, int nIndex) { int i; @@ -5680,20 +5695,6 @@ BOOL GetItemRecord(int nSeed, WORD wCI, int nIndex) return TRUE; } -void NextItemRecord(int i) -{ - gnNumGetRecords--; - - if (gnNumGetRecords == 0) { - return; - } - - itemrecord[i].dwTimestamp = itemrecord[gnNumGetRecords].dwTimestamp; - itemrecord[i].nSeed = itemrecord[gnNumGetRecords].nSeed; - itemrecord[i].wCI = itemrecord[gnNumGetRecords].wCI; - itemrecord[i].nIndex = itemrecord[gnNumGetRecords].nIndex; -} - void SetItemRecord(int nSeed, WORD wCI, int nIndex) { DWORD dwTicks; diff --git a/Source/items.h b/Source/items.h index 05827316d..a0c503c2f 100644 --- a/Source/items.h +++ b/Source/items.h @@ -9,20 +9,16 @@ extern int itemactive[MAXITEMS]; extern BOOL uitemflag; extern int itemavail[MAXITEMS]; -extern ItemStruct curruitem; extern ItemGetRecordStruct itemrecord[MAXITEMS]; extern ItemStruct item[MAXITEMS + 1]; -extern BOOL itemhold[3][3]; #ifdef HELLFIRE extern CornerStoneStruct CornerStone; #endif -extern BYTE *itemanims[ITEMTYPES]; extern BOOL UniqueItemFlag[128]; #ifdef HELLFIRE extern int auricGold; #endif extern int numitems; -extern int gnNumGetRecords; #ifdef HELLFIRE int items_4231CA(int i); @@ -35,63 +31,29 @@ int items_42342E(int i); int items_4234B2(int i); int items_423518(int i); int items_42357E(int i); -void items_42390F(); -int items_get_currlevel(); #endif void InitItemGFX(); -BOOL ItemPlace(int xp, int yp); -void AddInitItems(); void InitItems(); void CalcPlrItemVals(int p, BOOL Loadgfx); void CalcPlrScrolls(int p); void CalcPlrStaff(int p); -void CalcSelfItems(int pnum); -void CalcPlrItemMin(int pnum); -BOOL ItemMinStats(PlayerStruct *p, ItemStruct *x); -void CalcPlrBookVals(int p); void CalcPlrInv(int p, BOOL Loadgfx); void SetPlrHandItem(ItemStruct *h, int idata); void GetPlrHandSeed(ItemStruct *h); void GetGoldSeed(int pnum, ItemStruct *h); -void SetPlrHandSeed(ItemStruct *h, int iseed); void SetPlrHandGoldCurs(ItemStruct *h); void CreatePlrItems(int p); BOOL ItemSpaceOk(int i, int j); -BOOL GetItemSpace(int x, int y, char inum); -void GetSuperItemSpace(int x, int y, char inum); void GetSuperItemLoc(int x, int y, int &xx, int &yy); -void CalcItemValue(int i); -void GetBookSpell(int i, int lvl); -void GetStaffPower(int i, int lvl, int bs, BOOL onlygood); -void GetStaffSpell(int i, int lvl, BOOL onlygood); void GetItemAttrs(int i, int idata, int lvl); -int RndPL(int param1, int param2); -int PLVal(int pv, int p1, int p2, int minv, int maxv); void SaveItemPower(int i, int power, int param1, int param2, int minval, int maxval, int multval); void GetItemPower(int i, int minlvl, int maxlvl, int flgs, BOOL onlygood); -#ifdef HELLFIRE -void GetItemBonus(int i, int idata, int minlvl, int maxlvl, BOOL onlygood, BOOLEAN allowspells); -#else -void GetItemBonus(int i, int idata, int minlvl, int maxlvl, BOOL onlygood); -#endif void SetupItem(int i); int RndItem(int m); -int RndUItem(int m); -int RndAllItems(); -#ifdef HELLFIRE -int RndTypeItems(int itype, int imid, int lvl); -#else -int RndTypeItems(int itype, int imid); -#endif -int CheckUnique(int i, int lvl, int uper, BOOL recreate); -void GetUniqueItem(int i, int uid); void SpawnUnique(int uid, int x, int y); -void ItemRndDur(int ii); -void SetupAllItems(int ii, int idx, int iseed, int lvl, int uper, BOOL onlygood, BOOL recreate, BOOL pregen); void SpawnItem(int m, int x, int y, BOOL sendmsg); void CreateItem(int uid, int x, int y); void CreateRndItem(int x, int y, BOOL onlygood, BOOL sendmsg, BOOL delta); -void SetupAllUseful(int ii, int iseed, int lvl); void CreateRndUseful(int pnum, int x, int y, BOOL sendmsg); void CreateTypeItem(int x, int y, BOOL onlygood, int itype, int imisc, BOOL sendmsg, BOOL delta); void RecreateItem(int ii, int idx, WORD icreateinfo, int iseed, int ivalue); @@ -110,7 +72,6 @@ void SpawnTheodore(int xx, int yy); #endif void RespawnItem(int i, BOOL FlipFlag); void DeleteItem(int ii, int i); -void ItemDoppel(); void ProcessItems(); void FreeItemGFX(); void GetItemFrm(int i); @@ -121,50 +82,25 @@ void RepairItem(ItemStruct *i, int lvl); void DoRecharge(int pnum, int cii); #ifdef HELLFIRE void DoOil(int pnum, int cii); -BOOL OilItem(ItemStruct *x, PlayerStruct *p); #endif void RechargeItem(ItemStruct *i, int r); -void PrintItemOil(char IDidx); void PrintItemPower(char plidx, ItemStruct *x); -void DrawUTextBack(); -void PrintUString(int x, int y, BOOL cjustflag, const char *str, int col); -void DrawULine(int y); void DrawUniqueInfo(); -void PrintItemMisc(ItemStruct *x); void PrintItemDetails(ItemStruct *x); void PrintItemDur(ItemStruct *x); void UseItem(int p, int Mid, int spl); BOOL StoreStatOk(ItemStruct *h); -BOOL SmithItemOk(int i); -int RndSmithItem(int lvl); -void BubbleSwapItem(ItemStruct *a, ItemStruct *b); -void SortSmith(); void SpawnSmith(int lvl); -BOOL PremiumItemOk(int i); -int RndPremiumItem(int minlvl, int maxlvl); -void SpawnOnePremium(int i, int plvl); #ifdef HELLFIRE void SpawnPremium(int pnum); #else void SpawnPremium(int lvl); #endif -BOOL WitchItemOk(int i); -int RndWitchItem(int lvl); -void SortWitch(); void WitchBookLevel(int ii); void SpawnWitch(int lvl); -int RndBoyItem(int lvl); void SpawnBoy(int lvl); -BOOL HealerItemOk(int i); -int RndHealerItem(int lvl); -void SortHealer(); void SpawnHealer(int lvl); void SpawnStoreGold(); -void RecreateSmithItem(int ii, int idx, int lvl, int iseed); -void RecreatePremiumItem(int ii, int idx, int plvl, int iseed); -void RecreateBoyItem(int ii, int idx, int lvl, int iseed); -void RecreateWitchItem(int ii, int idx, int lvl, int iseed); -void RecreateHealerItem(int ii, int idx, int lvl, int iseed); void RecreateTownItem(int ii, int idx, WORD icreateinfo, int iseed, int ivalue); void RecalcStoreStats(); int ItemNoFlippy(); @@ -175,26 +111,16 @@ void CreateAmulet(int x, int y, int curlv, BOOL sendmsg, BOOL delta); #endif void CreateMagicWeapon(int x, int y, int imisc, int icurs, BOOL sendmsg, BOOL delta); BOOL GetItemRecord(int nSeed, WORD wCI, int nIndex); -void NextItemRecord(int i); void SetItemRecord(int nSeed, WORD wCI, int nIndex); void PutItemRecord(int nSeed, WORD wCI, int nIndex); /* data */ #ifdef HELLFIRE -extern int OilLevels[10]; -extern int OilValues[10]; -extern int OilMagic[10]; -extern char OilNames[10][25]; extern int MaxGold; #endif extern BYTE ItemCAnimTbl[]; -extern const char *const ItemDropNames[]; -extern BYTE ItemAnimLs[]; -extern int ItemDropSnds[]; extern int ItemInvSnds[]; -extern int idoppely; -extern int premiumlvladd[6]; #endif /* __ITEMS_H__ */ From 6b2a430e3bb39bb7230efd60263d205ebca5c4c3 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Tue, 3 Nov 2020 02:52:59 +0100 Subject: [PATCH 04/12] Clean up lighting.h --- Source/lighting.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Source/lighting.h b/Source/lighting.h index ac4b12783..82b4e8d9c 100644 --- a/Source/lighting.h +++ b/Source/lighting.h @@ -15,14 +15,11 @@ extern BOOL dovision; extern int numvision; extern char lightmax; extern BOOL dolighting; -extern BYTE lightblock[64][16][16]; extern int visionid; extern BYTE *pLightTbl; extern BOOL lightflag; -void RotateRadius(int *x, int *y, int *dx, int *dy, int *lx, int *ly, int *bx, int *by); void DoLighting(int nXPos, int nYPos, int nRadius, int Lnum); -void DoUnLight(int nXPos, int nYPos, int nRadius); void DoUnVision(int nXPos, int nYPos, int nRadius); void DoVision(int nXPos, int nYPos, int nRadius, BOOL doautomap, BOOL visible); void FreeLightTable(); @@ -52,9 +49,6 @@ void lighting_color_cycling(); /* rdata */ extern char CrawlTable[2749]; -extern char *pCrawlTable[19]; extern BYTE vCrawlTable[23][30]; -extern BYTE byte_49463C[18][18]; -extern BYTE RadiusAdj[23]; #endif /* __LIGHTING_H__ */ From a9502d43fc45738faccb0a1107dcecb00747d688 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Tue, 3 Nov 2020 03:12:27 +0100 Subject: [PATCH 05/12] Reorder loadsave.cpp --- Source/loadsave.cpp | 286 ++++++++++++++++++++++---------------------- Source/loadsave.h | 30 ----- 2 files changed, 145 insertions(+), 171 deletions(-) diff --git a/Source/loadsave.cpp b/Source/loadsave.cpp index f5b08d6a8..105b0e7a9 100644 --- a/Source/loadsave.cpp +++ b/Source/loadsave.cpp @@ -7,6 +7,106 @@ BYTE *tbuff; +static char BLoad() +{ + return *tbuff++; +} + +static int WLoad() +{ + int rv = *tbuff++ << 24; + rv |= *tbuff++ << 16; + rv |= *tbuff++ << 8; + rv |= *tbuff++; + + return rv; +} + +static int ILoad() +{ + int rv = *tbuff++ << 24; + rv |= *tbuff++ << 16; + rv |= *tbuff++ << 8; + rv |= *tbuff++; + + return rv; +} + +static BOOL OLoad() +{ + if (*tbuff++ == TRUE) + return TRUE; + else + return FALSE; +} + +static void LoadPlayer(int i) +{ + memcpy(&plr[i], tbuff, sizeof(*plr) - (10 * sizeof(void *))); + tbuff += sizeof(*plr) - (10 * sizeof(void *)); // omit last 10 pointers +} + +static void LoadMonster(int i) +{ + memcpy(&monster[i], tbuff, sizeof(*monster) - (3 * sizeof(void *))); + tbuff += sizeof(*monster) - (3 * sizeof(void *)); // omit last 3 pointers + SyncMonsterAnim(i); +} + +static void LoadMissile(int i) +{ + memcpy(&missile[i], tbuff, sizeof(*missile)); + tbuff += sizeof(*missile); +} + +static void LoadObject(int i) +{ + memcpy(&object[i], tbuff, sizeof(*object)); + tbuff += sizeof(*object); +} + +static void LoadItem(int i) +{ + memcpy(&item[i], tbuff, sizeof(*item)); + tbuff += sizeof(*item); + GetItemFrm(i); +} + +static void LoadPremium(int i) +{ + memcpy(&premiumitem[i], tbuff, sizeof(*premiumitem)); + tbuff += sizeof(*premiumitem); +} + +static void LoadQuest(int i) +{ + memcpy(&quests[i], tbuff, sizeof(*quests)); + tbuff += sizeof(*quests); + ReturnLvlX = WLoad(); + ReturnLvlY = WLoad(); + ReturnLvl = WLoad(); + ReturnLvlT = WLoad(); + DoomQuestState = WLoad(); +} + +static void LoadLighting(int i) +{ + memcpy(&LightList[i], tbuff, sizeof(*LightList)); + tbuff += sizeof(*LightList); +} + +static void LoadVision(int i) +{ + memcpy(&VisionList[i], tbuff, sizeof(*VisionList)); + tbuff += sizeof(*VisionList); +} + +static void LoadPortal(int i) +{ + memcpy(&portal[i], tbuff, sizeof(*portal)); + tbuff += sizeof(*portal); +} + /** * @brief Load game state * @param firstflag Can be set to false if we are simply reloading the current game @@ -27,6 +127,8 @@ void LoadGame(BOOL firstflag) #ifdef HELLFIRE if (ILoad() != 'HELF') +#elif defined(SPAWN) + if (ILoad() != 'SHAR') #else if (ILoad() != 'RETL') #endif @@ -190,103 +292,97 @@ void LoadGame(BOOL firstflag) gbProcessPlayers = TRUE; } -char BLoad() +static void BSave(char v) { - return *tbuff++; + *tbuff++ = v; } -int WLoad() +static void WSave(int v) { - int rv = *tbuff++ << 24; - rv |= *tbuff++ << 16; - rv |= *tbuff++ << 8; - rv |= *tbuff++; - - return rv; + *tbuff++ = v >> 24; + *tbuff++ = v >> 16; + *tbuff++ = v >> 8; + *tbuff++ = v; } -int ILoad() +static void ISave(int v) { - int rv = *tbuff++ << 24; - rv |= *tbuff++ << 16; - rv |= *tbuff++ << 8; - rv |= *tbuff++; - - return rv; + *tbuff++ = v >> 24; + *tbuff++ = v >> 16; + *tbuff++ = v >> 8; + *tbuff++ = v; } -BOOL OLoad() +static void OSave(BOOL v) { - if (*tbuff++ == TRUE) - return TRUE; + if (v != FALSE) + *tbuff++ = TRUE; else - return FALSE; + *tbuff++ = FALSE; } -void LoadPlayer(int i) +static void SavePlayer(int i) { - memcpy(&plr[i], tbuff, sizeof(*plr) - (10 * sizeof(void *))); + memcpy(tbuff, &plr[i], sizeof(*plr) - (10 * sizeof(void *))); tbuff += sizeof(*plr) - (10 * sizeof(void *)); // omit last 10 pointers } -void LoadMonster(int i) +static void SaveMonster(int i) { - memcpy(&monster[i], tbuff, sizeof(*monster) - (3 * sizeof(void *))); + memcpy(tbuff, &monster[i], sizeof(*monster) - (3 * sizeof(void *))); tbuff += sizeof(*monster) - (3 * sizeof(void *)); // omit last 3 pointers - SyncMonsterAnim(i); } -void LoadMissile(int i) +static void SaveMissile(int i) { - memcpy(&missile[i], tbuff, sizeof(*missile)); + memcpy(tbuff, &missile[i], sizeof(*missile)); tbuff += sizeof(*missile); } -void LoadObject(int i) +static void SaveObject(int i) { - memcpy(&object[i], tbuff, sizeof(*object)); + memcpy(tbuff, &object[i], sizeof(*object)); tbuff += sizeof(*object); } -void LoadItem(int i) +static void SaveItem(int i) { - memcpy(&item[i], tbuff, sizeof(*item)); + memcpy(tbuff, &item[i], sizeof(*item)); tbuff += sizeof(*item); - GetItemFrm(i); } -void LoadPremium(int i) +static void SavePremium(int i) { - memcpy(&premiumitem[i], tbuff, sizeof(*premiumitem)); + memcpy(tbuff, &premiumitem[i], sizeof(*premiumitem)); tbuff += sizeof(*premiumitem); } -void LoadQuest(int i) +static void SaveQuest(int i) { - memcpy(&quests[i], tbuff, sizeof(*quests)); + memcpy(tbuff, &quests[i], sizeof(*quests)); tbuff += sizeof(*quests); - ReturnLvlX = WLoad(); - ReturnLvlY = WLoad(); - ReturnLvl = WLoad(); - ReturnLvlT = WLoad(); - DoomQuestState = WLoad(); + WSave(ReturnLvlX); + WSave(ReturnLvlY); + WSave(ReturnLvl); + WSave(ReturnLvlT); + WSave(DoomQuestState); } -void LoadLighting(int i) +static void SaveLighting(int i) { - memcpy(&LightList[i], tbuff, sizeof(*LightList)); + memcpy(tbuff, &LightList[i], sizeof(*LightList)); tbuff += sizeof(*LightList); } -void LoadVision(int i) +static void SaveVision(int i) { - memcpy(&VisionList[i], tbuff, sizeof(*VisionList)); + memcpy(tbuff, &VisionList[i], sizeof(*VisionList)); tbuff += sizeof(*VisionList); } -void LoadPortal(int i) +static void SavePortal(int i) { - memcpy(&portal[i], tbuff, sizeof(*portal)); + memcpy(tbuff, &portal[i], sizeof(*portal)); tbuff += sizeof(*portal); } @@ -301,6 +397,8 @@ void SaveGame() #ifdef HELLFIRE ISave('HELF'); +#elif defined(SPAWN) + ISave('SHAR'); #else ISave('RETL'); #endif @@ -440,100 +538,6 @@ void SaveGame() pfile_write_hero(); } -void BSave(char v) -{ - *tbuff++ = v; -} - -void WSave(int v) -{ - *tbuff++ = v >> 24; - *tbuff++ = v >> 16; - *tbuff++ = v >> 8; - *tbuff++ = v; -} - -void ISave(int v) -{ - *tbuff++ = v >> 24; - *tbuff++ = v >> 16; - *tbuff++ = v >> 8; - *tbuff++ = v; -} - -void OSave(BOOL v) -{ - if (v != FALSE) - *tbuff++ = TRUE; - else - *tbuff++ = FALSE; -} - -void SavePlayer(int i) -{ - memcpy(tbuff, &plr[i], sizeof(*plr) - (10 * sizeof(void *))); - tbuff += sizeof(*plr) - (10 * sizeof(void *)); // omit last 10 pointers -} - -void SaveMonster(int i) -{ - memcpy(tbuff, &monster[i], sizeof(*monster) - (3 * sizeof(void *))); - tbuff += sizeof(*monster) - (3 * sizeof(void *)); // omit last 3 pointers -} - -void SaveMissile(int i) -{ - memcpy(tbuff, &missile[i], sizeof(*missile)); - tbuff += sizeof(*missile); -} - -void SaveObject(int i) -{ - memcpy(tbuff, &object[i], sizeof(*object)); - tbuff += sizeof(*object); -} - -void SaveItem(int i) -{ - memcpy(tbuff, &item[i], sizeof(*item)); - tbuff += sizeof(*item); -} - -void SavePremium(int i) -{ - memcpy(tbuff, &premiumitem[i], sizeof(*premiumitem)); - tbuff += sizeof(*premiumitem); -} - -void SaveQuest(int i) -{ - memcpy(tbuff, &quests[i], sizeof(*quests)); - tbuff += sizeof(*quests); - WSave(ReturnLvlX); - WSave(ReturnLvlY); - WSave(ReturnLvl); - WSave(ReturnLvlT); - WSave(DoomQuestState); -} - -void SaveLighting(int i) -{ - memcpy(tbuff, &LightList[i], sizeof(*LightList)); - tbuff += sizeof(*LightList); -} - -void SaveVision(int i) -{ - memcpy(tbuff, &VisionList[i], sizeof(*VisionList)); - tbuff += sizeof(*VisionList); -} - -void SavePortal(int i) -{ - memcpy(tbuff, &portal[i], sizeof(*portal)); - tbuff += sizeof(*portal); -} - void SaveLevel() { int i, j; diff --git a/Source/loadsave.h b/Source/loadsave.h index 376d4af36..9ecc9624a 100644 --- a/Source/loadsave.h +++ b/Source/loadsave.h @@ -6,38 +6,8 @@ #ifndef __LOADSAVE_H__ #define __LOADSAVE_H__ -extern BYTE *tbuff; - void LoadGame(BOOL firstflag); -char BLoad(); -int WLoad(); -int ILoad(); -BOOL OLoad(); -void LoadPlayer(int i); -void LoadMonster(int i); -void LoadMissile(int i); -void LoadObject(int i); -void LoadItem(int i); -void LoadPremium(int i); -void LoadQuest(int i); -void LoadLighting(int i); -void LoadVision(int i); -void LoadPortal(int i); void SaveGame(); -void BSave(char v); -void WSave(int v); -void ISave(int v); -void OSave(BOOL v); -void SavePlayer(int i); -void SaveMonster(int i); -void SaveMissile(int i); -void SaveObject(int i); -void SaveItem(int i); -void SavePremium(int i); -void SaveQuest(int i); -void SaveLighting(int i); -void SaveVision(int i); -void SavePortal(int i); void SaveLevel(); void LoadLevel(); From 7a11d63db30f44f5a401d39cba5e9c293824e766 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Tue, 3 Nov 2020 03:32:48 +0100 Subject: [PATCH 06/12] Reorder logging.cpp --- Source/logging.cpp | 70 +++++++++++++++++++++++----------------------- Source/logging.h | 6 ---- 2 files changed, 35 insertions(+), 41 deletions(-) diff --git a/Source/logging.cpp b/Source/logging.cpp index 2824e5a9c..a623d4217 100644 --- a/Source/logging.cpp +++ b/Source/logging.cpp @@ -19,31 +19,31 @@ BOOL log_not_created = TRUE; /** Handle to the log file. */ HANDLE log_file = INVALID_HANDLE_VALUE; -void __cdecl log_flush(BOOL force_close) +static void log_get_version(VS_FIXEDFILEINFO *file_info) { - DWORD NumberOfBytesWritten; + DWORD size, len, dwHandle; + unsigned int puLen; + void *version; + char Filename[MAX_PATH]; + LPVOID lpBuffer; - sgMemCrit.Enter(); - if (nNumberOfBytesToWrite) { - if (log_file == INVALID_HANDLE_VALUE) { - log_file = log_create(); - if (log_file == INVALID_HANDLE_VALUE) { - nNumberOfBytesToWrite = 0; - return; + memset(file_info, 0, sizeof(*file_info)); + if (GetModuleFileName(0, Filename, sizeof(Filename))) { + size = GetFileVersionInfoSize(Filename, &dwHandle); + if (size) { + version = VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); + if (GetFileVersionInfo(Filename, 0, size, version) && VerQueryValue(version, "\\", &lpBuffer, &puLen)) { + len = puLen; + if (puLen >= 52) + len = 52; + memcpy(file_info, lpBuffer, len); } - SetFilePointer(log_file, 0, NULL, FILE_END); + VirtualFree(version, 0, MEM_RELEASE); } - WriteFile(log_file, lpAddress, nNumberOfBytesToWrite, &NumberOfBytesWritten, 0); - nNumberOfBytesToWrite = 0; - } - if (force_close && log_file != INVALID_HANDLE_VALUE) { - CloseHandle(log_file); - log_file = INVALID_HANDLE_VALUE; } - sgMemCrit.Leave(); } -HANDLE log_create() +static HANDLE log_create() { char *last_slash_pos; HANDLE fh; @@ -95,28 +95,28 @@ HANDLE log_create() return fh; } -void log_get_version(VS_FIXEDFILEINFO *file_info) +void __cdecl log_flush(BOOL force_close) { - DWORD size, len, dwHandle; - unsigned int puLen; - void *version; - char Filename[MAX_PATH]; - LPVOID lpBuffer; + DWORD NumberOfBytesWritten; - memset(file_info, 0, sizeof(*file_info)); - if (GetModuleFileName(0, Filename, sizeof(Filename))) { - size = GetFileVersionInfoSize(Filename, &dwHandle); - if (size) { - version = VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); - if (GetFileVersionInfo(Filename, 0, size, version) && VerQueryValue(version, "\\", &lpBuffer, &puLen)) { - len = puLen; - if (puLen >= 52) - len = 52; - memcpy(file_info, lpBuffer, len); + sgMemCrit.Enter(); + if (nNumberOfBytesToWrite) { + if (log_file == INVALID_HANDLE_VALUE) { + log_file = log_create(); + if (log_file == INVALID_HANDLE_VALUE) { + nNumberOfBytesToWrite = 0; + return; } - VirtualFree(version, 0, MEM_RELEASE); + SetFilePointer(log_file, 0, NULL, FILE_END); } + WriteFile(log_file, lpAddress, nNumberOfBytesToWrite, &NumberOfBytesWritten, 0); + nNumberOfBytesToWrite = 0; + } + if (force_close && log_file != INVALID_HANDLE_VALUE) { + CloseHandle(log_file); + log_file = INVALID_HANDLE_VALUE; } + sgMemCrit.Leave(); } void __cdecl log_printf(const char *pszFmt, ...) diff --git a/Source/logging.h b/Source/logging.h index b88d8e42e..451328c41 100644 --- a/Source/logging.h +++ b/Source/logging.h @@ -7,21 +7,15 @@ #define __LOGGING_H__ extern CHAR FileName[MAX_PATH]; -extern char log_buffer[388]; extern LPCVOID lpAddress; extern DWORD nNumberOfBytesToWrite; void __cdecl log_flush(BOOL force_close); -HANDLE log_create(); -void log_get_version(VS_FIXEDFILEINFO *file_info); void __cdecl log_printf(const char *pszFmt, ...); // LogMessage void log_dump_computer_info(); -/* rdata */ - /* data */ -extern int log_not_created; extern HANDLE log_file; #endif /* __LOGGING_H__ */ From ead99489b11a9817f84bc7bfeffd7474bc23df8a Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Tue, 3 Nov 2020 03:53:09 +0100 Subject: [PATCH 07/12] Reorder mainmenu.cpp --- Source/mainmenu.cpp | 204 ++++++++++++++++++++++---------------------- Source/mainmenu.h | 11 --- 2 files changed, 102 insertions(+), 113 deletions(-) diff --git a/Source/mainmenu.cpp b/Source/mainmenu.cpp index c2c1c1cd2..a59d01c7a 100644 --- a/Source/mainmenu.cpp +++ b/Source/mainmenu.cpp @@ -26,6 +26,108 @@ void mainmenu_refresh_music() #endif } +static BOOL mainmenu_init_menu(int type) +{ + BOOL success; + + if (type == SELHERO_PREVIOUS) + return TRUE; + + music_stop(); + + success = StartGame(type != SELHERO_CONTINUE, type != SELHERO_CONNECT); + if (success) + mainmenu_refresh_music(); + + return success; +} + +static BOOL mainmenu_single_player() +{ +#ifdef HELLFIRE + int dlgresult; + + while (TRUE) { + gbMaxPlayers = 1; + dlgresult = 0; + + if (!SRegLoadValue(APP_NAME, jogging_title, 0, &jogging_opt)) { + jogging_opt = TRUE; + } + + if (!UiSelHeroSingDialog( + pfile_ui_set_hero_infos, + pfile_ui_save_create, + pfile_delete_save, + pfile_ui_set_class_stats, + &dlgresult, + gszHero, + &gnDifficulty + //,UseBardTest, + //UseBarbarianTest + )) { + app_fatal("Unable to display SelHeroSing"); + } + + if (dlgresult == SELHERO_PREVIOUS) + return TRUE; + if (!mainmenu_init_menu(dlgresult)) + return FALSE; + } +#else + gbMaxPlayers = 1; + return mainmenu_init_menu(SELHERO_NEW_DUNGEON); +#endif +} + +static BOOL mainmenu_multi_player() +{ +#ifdef HELLFIRE + int dlgresult; + BOOL hero_is_created = TRUE; + + while (TRUE) { + gbMaxPlayers = MAX_PLRS; + dlgresult = 0; + jogging_opt = FALSE; + if (!UiSelHeroMultDialog( + pfile_ui_set_hero_infos, + pfile_ui_save_create, + pfile_delete_save, + pfile_ui_set_class_stats, + &dlgresult, + &hero_is_created, // Not in hellfire + gszHero + //,UseBardTest, + //UseBarbarianTest + )) { + app_fatal("Can't load multiplayer dialog"); + } + + if (dlgresult == SELHERO_PREVIOUS) + return TRUE; + if (!mainmenu_init_menu(dlgresult)) + return FALSE; + } +#else + gbMaxPlayers = MAX_PLRS; + return mainmenu_init_menu(SELHERO_CONNECT); +#endif +} + +#ifndef SPAWN +static void mainmenu_play_intro() +{ + music_stop(); +#ifdef HELLFIRE + play_movie("gendata\\Hellfire.smk", TRUE); +#else + play_movie("gendata\\diablo1.smk", TRUE); +#endif + mainmenu_refresh_music(); +} +#endif + void __stdcall mainmenu_change_name(int arg1, int arg2, int arg3, int arg4, char *name_1, char *name_2) { if (UiValidPlayerName(name_2)) @@ -148,105 +250,3 @@ void mainmenu_loop() music_stop(); } - -BOOL mainmenu_single_player() -{ -#ifdef HELLFIRE - int dlgresult; - - while (TRUE) { - gbMaxPlayers = 1; - dlgresult = 0; - - if (!SRegLoadValue(APP_NAME, jogging_title, 0, &jogging_opt)) { - jogging_opt = TRUE; - } - - if (!UiSelHeroSingDialog( - pfile_ui_set_hero_infos, - pfile_ui_save_create, - pfile_delete_save, - pfile_ui_set_class_stats, - &dlgresult, - gszHero, - &gnDifficulty - //,UseBardTest, - //UseBarbarianTest - )) { - app_fatal("Unable to display SelHeroSing"); - } - - if (dlgresult == SELHERO_PREVIOUS) - return TRUE; - if (!mainmenu_init_menu(dlgresult)) - return FALSE; - } -#else - gbMaxPlayers = 1; - return mainmenu_init_menu(SELHERO_NEW_DUNGEON); -#endif -} - -BOOL mainmenu_init_menu(int type) -{ - BOOL success; - - if (type == SELHERO_PREVIOUS) - return TRUE; - - music_stop(); - - success = StartGame(type != SELHERO_CONTINUE, type != SELHERO_CONNECT); - if (success) - mainmenu_refresh_music(); - - return success; -} - -BOOL mainmenu_multi_player() -{ -#ifdef HELLFIRE - int dlgresult; - BOOL hero_is_created = TRUE; - - while (TRUE) { - gbMaxPlayers = MAX_PLRS; - dlgresult = 0; - jogging_opt = FALSE; - if (!UiSelHeroMultDialog( - pfile_ui_set_hero_infos, - pfile_ui_save_create, - pfile_delete_save, - pfile_ui_set_class_stats, - &dlgresult, - &hero_is_created, // Not in hellfire - gszHero - //,UseBardTest, - //UseBarbarianTest - )) { - app_fatal("Can't load multiplayer dialog"); - } - - if (dlgresult == SELHERO_PREVIOUS) - return TRUE; - if (!mainmenu_init_menu(dlgresult)) - return FALSE; - } -#else - gbMaxPlayers = MAX_PLRS; - return mainmenu_init_menu(SELHERO_CONNECT); -#endif -} - -#ifndef SPAWN -void mainmenu_play_intro() -{ - music_stop(); -#ifdef HELLFIRE - play_movie("gendata\\Hellfire.smk", TRUE); -#else - play_movie("gendata\\diablo1.smk", TRUE); -#endif - mainmenu_refresh_music(); -} -#endif diff --git a/Source/mainmenu.h b/Source/mainmenu.h index 112676219..c29f75920 100644 --- a/Source/mainmenu.h +++ b/Source/mainmenu.h @@ -8,7 +8,6 @@ extern char gszHero[16]; -void mainmenu_refresh_music(); void __stdcall mainmenu_change_name(int arg1, int arg2, int arg3, int arg4, char *name_1, char *name_2); BOOL __stdcall mainmenu_select_hero_dialog( const _SNETPROGRAMDATA *client_info, @@ -20,15 +19,5 @@ BOOL __stdcall mainmenu_select_hero_dialog( char *cdesc, DWORD cdlen, // character "description" will be copied here (used to advertise games) BOOL *multi); // new character? - unsure about this void mainmenu_loop(); -BOOL mainmenu_single_player(); -BOOL mainmenu_init_menu(int type); -BOOL mainmenu_multi_player(); -void mainmenu_play_intro(); - -/* rdata */ - -/* data */ - -extern int menu_music_track_id; #endif /* __MAINMENU_H__ */ From 310dc0b1e7addd4aa40931d63e56f6b84a51cce9 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Tue, 3 Nov 2020 03:56:44 +0100 Subject: [PATCH 08/12] Clean up minitext.h --- Source/minitext.h | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/Source/minitext.h b/Source/minitext.h index effcd18d0..fd857e495 100644 --- a/Source/minitext.h +++ b/Source/minitext.h @@ -6,26 +6,12 @@ #ifndef __MINITEXT_H__ #define __MINITEXT_H__ -extern int qtexty; -extern const char *qtextptr; extern BOOLEAN qtextflag; -extern BYTE *pMedTextCels; -extern BYTE *pTextBoxCels; void FreeQuestText(); void InitQuestText(); void InitQTextMsg(int m); void DrawQTextBack(); -void PrintQTextChr(int sx, int sy, BYTE *pCelBuff, int nCel); void DrawQText(); -/* rdata */ - -extern const BYTE mfontframe[128]; -extern const BYTE mfontkern[56]; - -/* data */ - -extern int qscroll_spd_tbl[9]; - #endif /* __MINITEXT_H__ */ From 7080786f33a8e95e0c6e83b221b57a99e8c2f68f Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Tue, 3 Nov 2020 18:05:26 +0100 Subject: [PATCH 09/12] Clean up missiles.h --- Source/missiles.cpp | 36 ++++++++++++++++++------------------ Source/missiles.h | 25 ------------------------- 2 files changed, 18 insertions(+), 43 deletions(-) diff --git a/Source/missiles.cpp b/Source/missiles.cpp index 0c8e4e3a3..c8cfba850 100644 --- a/Source/missiles.cpp +++ b/Source/missiles.cpp @@ -1313,24 +1313,7 @@ void missiles_hive_explosion(int mi, int sx, int sy, int dx, int dy, int midir, missile[mi]._miDelFlag = TRUE; } -void missiles_fire_rune(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam) -{ - if (LineClear(sx, sy, dx, dy)) { - if (id >= 0) - UseMana(id, SPL_RUNEFIRE); - if (missiles_found_target(mi, &dx, &dy, 10)) { - missile[mi]._miVar1 = MIS_HIVEEXP; - missile[mi]._miDelFlag = FALSE; - missile[mi]._mlid = AddLight(dx, dy, 8); - } else { - missile[mi]._miDelFlag = TRUE; - } - } else { - missile[mi]._miDelFlag = TRUE; - } -} - -BOOLEAN missiles_found_target(int mi, int *x, int *y, int rad) +static BOOLEAN missiles_found_target(int mi, int *x, int *y, int rad) { BOOLEAN found; int i, j, k, tx, ty, dp; @@ -1364,6 +1347,23 @@ BOOLEAN missiles_found_target(int mi, int *x, int *y, int rad) return found; } +void missiles_fire_rune(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam) +{ + if (LineClear(sx, sy, dx, dy)) { + if (id >= 0) + UseMana(id, SPL_RUNEFIRE); + if (missiles_found_target(mi, &dx, &dy, 10)) { + missile[mi]._miVar1 = MIS_HIVEEXP; + missile[mi]._miDelFlag = FALSE; + missile[mi]._mlid = AddLight(dx, dy, 8); + } else { + missile[mi]._miDelFlag = TRUE; + } + } else { + missile[mi]._miDelFlag = TRUE; + } +} + void missiles_light_rune(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam) { if (LineClear(sx, sy, dx, dy)) { diff --git a/Source/missiles.h b/Source/missiles.h index b5d9c8717..122cad7e7 100644 --- a/Source/missiles.h +++ b/Source/missiles.h @@ -10,39 +10,23 @@ extern int missileactive[MAXMISSILES]; extern int missileavail[MAXMISSILES]; extern MissileStruct missile[MAXMISSILES]; extern int nummissiles; -extern BOOL ManashieldFlag; -extern ChainStruct chain[MAXMISSILES]; extern BOOL MissilePreFlag; -extern int numchains; void GetDamageAmt(int i, int *mind, int *maxd); -BOOL CheckBlock(int fx, int fy, int tx, int ty); -int FindClosest(int sx, int sy, int rad); int GetSpellLevel(int id, int sn); -int GetDirection8(int x1, int y1, int x2, int y2); -int GetDirection16(int x1, int y1, int x2, int y2); void DeleteMissile(int mi, int i); -void GetMissileVel(int i, int sx, int sy, int dx, int dy, int v); -void PutMissile(int i); -void GetMissilePos(int i); -void MoveMissilePos(int i); BOOL MonsterTrapHit(int m, int mindam, int maxdam, int dist, int t, BOOLEAN shift); -BOOL MonsterMHit(int pnum, int m, int mindam, int maxdam, int dist, int t, BOOLEAN shift); BOOL PlayerMHit(int pnum, int m, int dist, int mind, int maxd, int mtype, BOOLEAN shift, int earflag); -BOOL Plr2PlrMHit(int pnum, int p, int mindam, int maxdam, int dist, int mtype, BOOLEAN shift); -void CheckMissileCol(int i, int mindam, int maxdam, BOOL shift, int mx, int my, BOOLEAN nodel); void SetMissAnim(int mi, int animtype); void SetMissDir(int mi, int dir); void LoadMissileGFX(BYTE mi); void InitMissileGFX(); -void FreeMissileGFX(int mi); void FreeMissiles(); void FreeMissiles2(); void InitMissiles(); #ifdef HELLFIRE void missiles_hive_explosion(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam); void missiles_fire_rune(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam); -BOOLEAN missiles_found_target(int mi, int *x, int *y, int rad); void missiles_light_rune(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam); void missiles_great_light_rune(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam); void missiles_immolation_rune(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam); @@ -72,7 +56,6 @@ void AddBlodboil(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy #endif void AddLArrow(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam); void AddArrow(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam); -void GetVileMissPos(int mi, int dx, int dy); void AddRndTeleport(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam); void AddFirebolt(int mi, int sx, int sy, int dx, int dy, int midir, char micaster, int id, int dam); void AddMagmaball(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam); @@ -85,7 +68,6 @@ void AddLightctrl(int mi, int sx, int sy, int dx, int dy, int midir, char mienem void AddLightning(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam); void AddMisexp(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam); void AddWeapexp(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam); -BOOL CheckIfTrig(int x, int y); void AddTown(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam); void AddFlash(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam); void AddFlash2(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam); @@ -132,7 +114,6 @@ void AddBoneSpirit(int mi, int sx, int sy, int dx, int dy, int midir, char miene void AddRportal(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam); void AddDiabApoca(int mi, int sx, int sy, int dx, int dy, int midir, char mienemy, int id, int dam); int AddMissile(int sx, int sy, int dx, int dy, int midir, int mitype, char micaster, int id, int midam, int spllvl); -int Sentfire(int i, int sx, int sy); void MI_Dummy(int i); void MI_Golem(int i); void MI_SetManashield(int i); @@ -166,7 +147,6 @@ void MI_Lightning(int i); void MI_Town(int i); void MI_Flash(int i); void MI_Flash2(int i); -void MI_Manashield(int i); void MI_Etherealize(int i); void MI_Firemove(int i); void MI_Guardian(int i); @@ -198,9 +178,4 @@ void ProcessMissiles(); void missiles_process_charge(); void ClearMissileSpot(int mi); -/* rdata */ - -extern int XDirAdd[8]; -extern int YDirAdd[8]; - #endif /* __MISSILES_H__ */ From dca5a135b6bd29724e2b7026888951be572aede7 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Tue, 3 Nov 2020 19:26:03 +0100 Subject: [PATCH 10/12] Reorder monster.cpp --- Source/monster.cpp | 60 ++++++++++++++++----------------- Source/monster.h | 84 ---------------------------------------------- 2 files changed, 30 insertions(+), 114 deletions(-) diff --git a/Source/monster.cpp b/Source/monster.cpp index 000354e3e..073cb4457 100644 --- a/Source/monster.cpp +++ b/Source/monster.cpp @@ -924,6 +924,36 @@ void PlaceUniqueMonst(int uniqindex, int miniontype, int unpackfilesize) } } +static void PlaceUniques() +{ + int u, mt; + BOOL done; + + for (u = 0; UniqMonst[u].mtype != -1; u++) { + if (UniqMonst[u].mlevel != currlevel) + continue; + done = FALSE; + for (mt = 0; mt < nummtypes; mt++) { + if (done) + break; + done = (Monsters[mt].mtype == UniqMonst[u].mtype); + } + mt--; + if (u == UMT_GARBUD && quests[Q_GARBUD]._qactive == QUEST_NOTAVAIL) + done = FALSE; + if (u == UMT_ZHAR && quests[Q_ZHAR]._qactive == QUEST_NOTAVAIL) + done = FALSE; + if (u == UMT_SNOTSPIL && quests[Q_LTBANNER]._qactive == QUEST_NOTAVAIL) + done = FALSE; + if (u == UMT_LACHDAN && quests[Q_VEIL]._qactive == QUEST_NOTAVAIL) + done = FALSE; + if (u == UMT_WARLORD && quests[Q_WARLORD]._qactive == QUEST_NOTAVAIL) + done = FALSE; + if (done) + PlaceUniqueMonst(u, mt, 8); + } +} + void PlaceQuestMonsters() { int skeltype; @@ -1188,36 +1218,6 @@ void InitMonsters() } #ifndef SPAWN -void PlaceUniques() -{ - int u, mt; - BOOL done; - - for (u = 0; UniqMonst[u].mtype != -1; u++) { - if (UniqMonst[u].mlevel != currlevel) - continue; - done = FALSE; - for (mt = 0; mt < nummtypes; mt++) { - if (done) - break; - done = (Monsters[mt].mtype == UniqMonst[u].mtype); - } - mt--; - if (u == UMT_GARBUD && quests[Q_GARBUD]._qactive == QUEST_NOTAVAIL) - done = FALSE; - if (u == UMT_ZHAR && quests[Q_ZHAR]._qactive == QUEST_NOTAVAIL) - done = FALSE; - if (u == UMT_SNOTSPIL && quests[Q_LTBANNER]._qactive == QUEST_NOTAVAIL) - done = FALSE; - if (u == UMT_LACHDAN && quests[Q_VEIL]._qactive == QUEST_NOTAVAIL) - done = FALSE; - if (u == UMT_WARLORD && quests[Q_WARLORD]._qactive == QUEST_NOTAVAIL) - done = FALSE; - if (done) - PlaceUniqueMonst(u, mt, 8); - } -} - void SetMapMonsters(BYTE *pMap, int startx, int starty) { WORD rw, rh; diff --git a/Source/monster.h b/Source/monster.h index 91d7f6a7c..70fca6216 100644 --- a/Source/monster.h +++ b/Source/monster.h @@ -6,113 +6,49 @@ #ifndef __MONSTER_H__ #define __MONSTER_H__ -extern int MissileFileFlag; extern int monstkills[MAXMONSTERS]; extern int monstactive[MAXMONSTERS]; extern int nummonsters; extern MonsterStruct monster[MAXMONSTERS]; -extern int totalmonsters; extern CMonster Monsters[MAX_LVLMTYPES]; -#ifdef HELLFIRE -extern int GraphicTable[NUMLEVELS][MAX_LVLMTYPES]; -#else -extern BYTE GraphicTable[NUMLEVELS][MAX_LVLMTYPES]; -#endif -extern int monstimgtot; -extern int uniquetrans; extern int nummtypes; -void InitMonsterTRN(int monst, BOOL special); void InitLevelMonsters(); -int AddMonsterType(int type, int placeflag); void GetLevelMTypes(); void InitMonsterGFX(int monst); -void ClearMVars(int i); void InitMonster(int i, int rd, int mtype, int x, int y); void ClrAllMonsters(); -BOOL MonstPlace(int xp, int yp); #ifdef HELLFIRE void monster_some_crypt(); #endif -void PlaceMonster(int i, int mtype, int x, int y); -void PlaceUniqueMonst(int uniqindex, int miniontype, int unpackfilesize); -void PlaceQuestMonsters(); void PlaceGroup(int mtype, int num, int leaderf, int leader); -void LoadDiabMonsts(); void InitMonsters(); -void PlaceUniques(); void SetMapMonsters(BYTE *pMap, int startx, int starty); void DeleteMonster(int i); int AddMonster(int x, int y, int dir, int mtype, BOOL InMap); #ifdef HELLFIRE void monster_43C785(int i); #endif -void NewMonsterAnim(int i, AnimStruct &anim, int md); -BOOL M_Ranged(int i); BOOL M_Talker(int i); -void M_Enemy(int i); -int M_GetDir(int i); -void M_CheckEFlag(int i); void M_StartStand(int i, int md); -void M_StartDelay(int i, int len); -void M_StartSpStand(int i, int md); -void M_StartWalk(int i, int xvel, int yvel, int xadd, int yadd, int EndDir); -void M_StartWalk2(int i, int xvel, int yvel, int xoff, int yoff, int xadd, int yadd, int EndDir); -void M_StartWalk3(int i, int xvel, int yvel, int xoff, int yoff, int xadd, int yadd, int mapx, int mapy, int EndDir); -void M_StartAttack(int i); -void M_StartRAttack(int i, int missile_type, int dam); -void M_StartRSpAttack(int i, int missile_type, int dam); -void M_StartSpAttack(int i); -void M_StartEat(int i); void M_ClearSquares(int i); void M_GetKnockback(int i); void M_StartHit(int i, int pnum, int dam); -void M_DiabloDeath(int i, BOOL sendmsg); -#ifdef HELLFIRE -void SpawnLoot(int i, BOOL sendmsg); -#endif -void M2MStartHit(int mid, int i, int dam); -void MonstStartKill(int i, int pnum, BOOL sendmsg); -void M2MStartKill(int i, int mid); void M_StartKill(int i, int pnum); void M_SyncStartKill(int i, int x, int y, int pnum); -void M_StartFadein(int i, int md, BOOL backwards); -void M_StartFadeout(int i, int md, BOOL backwards); -void M_StartHeal(int i); -void M_ChangeLightOffset(int monst); BOOL M_DoStand(int i); BOOL M_DoWalk(int i); BOOL M_DoWalk2(int i); BOOL M_DoWalk3(int i); -void M_TryM2MHit(int i, int mid, int hper, int mind, int maxd); -void M_TryH2HHit(int i, int pnum, int Hit, int MinDam, int MaxDam); BOOL M_DoAttack(int i); -BOOL M_DoRAttack(int i); -int M_DoRSpAttack(int i); -BOOL M_DoSAttack(int i); -BOOL M_DoFadein(int i); -BOOL M_DoFadeout(int i); -int M_DoHeal(int i); -int M_DoTalk(int i); void M_Teleport(int i); BOOL M_DoGotHit(int i); void M_UpdateLeader(int i); void DoEnding(); void PrepDoEnding(); -BOOL M_DoDeath(int i); -BOOL M_DoSpStand(int i); -BOOL M_DoDelay(int i); -BOOL M_DoStone(int i); void M_WalkDir(int i, int md); -void GroupUnity(int i); -BOOL M_CallWalk(int i, int md); -BOOL M_PathWalk(int i); -BOOL M_CallWalk2(int i, int md); -BOOL M_DumbWalk(int i, int md); -BOOL M_RoundWalk(int i, int md, int &dir); void MAI_Zombie(int i); void MAI_SkelSd(int i); -BOOL MAI_Path(int i); void MAI_Snake(int i); void MAI_Bat(int i); void MAI_SkelBow(int i); @@ -147,7 +83,6 @@ void mai_roundranged_441EA0(int i); #endif void MAI_Acid(int i); void MAI_Diablo(int i); -void MAI_RR2(int i, int mistype, int dam); void MAI_Mega(int i); void MAI_Golum(int i); void MAI_SkelKing(int i); @@ -186,7 +121,6 @@ BOOL PosOkMonst3(int i, int x, int y); BOOL IsSkel(int mt); BOOL IsGoat(int mt); int M_SpawnSkel(int x, int y, int dir); -void ActivateSpawn(int i, int x, int y, int dir); BOOL SpawnSkeleton(int ii, int x, int y); int PreSpawnSkeleton(); void TalktoMonster(int i); @@ -196,28 +130,10 @@ BOOL CheckMonsterHit(int m, BOOL &ret); int encode_enemy(int m); void decode_enemy(int m, int enemy); -/* rdata */ - -extern const char plr2monst[9]; -extern const BYTE counsmiss[4]; - /* data */ -extern int MWVel[24][3]; -extern char animletter[7]; -extern int left[8]; -extern int right[8]; extern int opposite[8]; extern int offset_x[8]; extern int offset_y[8]; -/** unused */ -extern int rnd5[4]; -extern int rnd10[4]; -extern int rnd20[4]; -extern int rnd60[4]; -// - -extern void (*AiProc[])(int i); - #endif /* __MONSTER_H__ */ From e9553eedf747a03e09e064ca158808821865dba5 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Tue, 3 Nov 2020 19:51:12 +0100 Subject: [PATCH 11/12] Clean up movie.h --- Source/movie.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/Source/movie.h b/Source/movie.h index d99b1c343..ee34bbe0d 100644 --- a/Source/movie.h +++ b/Source/movie.h @@ -6,12 +6,9 @@ #ifndef __MOVIE_H__ #define __MOVIE_H__ -extern BYTE movie_playing; extern BOOL loop_movie; void play_movie(const char *pszMovie, BOOL user_can_close); LRESULT __stdcall MovieWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); -/* rdata */ - #endif /* __MOVIE_H__ */ From f2f4de8e3372b90a30a68ad6f5be47eeff1c5da6 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Wed, 4 Nov 2020 00:01:29 +0100 Subject: [PATCH 12/12] Reorder mpqapi.cpp --- Source/mpqapi.cpp | 532 +++++++++++++++++++++++----------------------- Source/mpqapi.h | 27 --- 2 files changed, 266 insertions(+), 293 deletions(-) diff --git a/Source/mpqapi.cpp b/Source/mpqapi.cpp index 6d1ca02a6..90cdd6dcc 100644 --- a/Source/mpqapi.cpp +++ b/Source/mpqapi.cpp @@ -21,39 +21,23 @@ BOOLEAN save_archive_open; HANDLE sghArchive = INVALID_HANDLE_VALUE; -BOOL mpqapi_set_hidden(const char *pszArchive, BOOL hidden) +static void mpqapi_xor_buf(char *pbData) { - DWORD dwFileAttributes; - DWORD dwFileAttributesToSet; - - dwFileAttributes = GetFileAttributes(pszArchive); - if (dwFileAttributes == INVALID_FILE_ATTRIBUTES) - return GetLastError() == ERROR_FILE_NOT_FOUND; - dwFileAttributesToSet = hidden ? FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN : 0; - if (dwFileAttributes == dwFileAttributesToSet) - return TRUE; - else - return SetFileAttributes(pszArchive, dwFileAttributesToSet); -} + DWORD mask; + char *pbCurrentData; + int i; -void mpqapi_store_creation_time(const char *pszArchive, DWORD dwChar) -{ - HANDLE handle; - struct _WIN32_FIND_DATAA FindFileData; - char dst[160]; + mask = 0xF0761AB; + pbCurrentData = pbData; - if (gbMaxPlayers != 1) { - mpqapi_reg_load_modification_time(dst, 160); - handle = FindFirstFile(pszArchive, &FindFileData); - if (handle != INVALID_HANDLE_VALUE) { - FindClose(handle); - *((FILETIME *)(dst) + dwChar * 2) = FindFileData.ftCreationTime; - mpqapi_reg_store_modification_time(dst, 160); - } + for (i = 0; i < 8; i++) { + *pbCurrentData ^= mask; + pbCurrentData++; + mask = _rotl(mask, 1); } } -BOOL mpqapi_reg_load_modification_time(char *dst, int size) +static BOOL mpqapi_reg_load_modification_time(char *dst, int size) { char *pszDst; char *pbData; @@ -81,20 +65,42 @@ BOOL mpqapi_reg_load_modification_time(char *dst, int size) return TRUE; } -void mpqapi_xor_buf(char *pbData) +static BOOLEAN mpqapi_reg_store_modification_time(char *pbData, DWORD dwLen) { - DWORD mask; - char *pbCurrentData; - int i; + char *pbCurrentData, *pbDataToXor; + DWORD i; - mask = 0xF0761AB; pbCurrentData = pbData; - - for (i = 0; i < 8; i++) { - *pbCurrentData ^= mask; - pbCurrentData++; - mask = _rotl(mask, 1); + if (dwLen >= 8) { + i = dwLen >> 3; + do { + pbDataToXor = pbCurrentData; + pbCurrentData += 8; + mpqapi_xor_buf(pbDataToXor); + i--; + } while (i); } + +#ifdef SPAWN + return SRegSaveData(APP_NAME, "Audio Playback ", 0, (BYTE *)pbData, dwLen); +#else + return SRegSaveData(APP_NAME, "Video Player ", 0, (BYTE *)pbData, dwLen); +#endif +} + +BOOL mpqapi_set_hidden(const char *pszArchive, BOOL hidden) +{ + DWORD dwFileAttributes; + DWORD dwFileAttributesToSet; + + dwFileAttributes = GetFileAttributes(pszArchive); + if (dwFileAttributes == INVALID_FILE_ATTRIBUTES) + return GetLastError() == ERROR_FILE_NOT_FOUND; + dwFileAttributesToSet = hidden ? FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN : 0; + if (dwFileAttributes == dwFileAttributesToSet) + return TRUE; + else + return SetFileAttributes(pszArchive, dwFileAttributesToSet); } void mpqapi_store_default_time(DWORD dwChar) @@ -115,49 +121,63 @@ void mpqapi_store_default_time(DWORD dwChar) */ } -BOOLEAN mpqapi_reg_store_modification_time(char *pbData, DWORD dwLen) +static void mpqapi_store_modified_time(const char *pszArchive, DWORD dwChar) { - char *pbCurrentData, *pbDataToXor; - DWORD i; + HANDLE handle; + struct _WIN32_FIND_DATAA FindFileData; + char dst[160]; - pbCurrentData = pbData; - if (dwLen >= 8) { - i = dwLen >> 3; - do { - pbDataToXor = pbCurrentData; - pbCurrentData += 8; - mpqapi_xor_buf(pbDataToXor); - i--; - } while (i); + if (gbMaxPlayers != 1) { + mpqapi_reg_load_modification_time(dst, 160); + handle = FindFirstFile(pszArchive, &FindFileData); + if (handle != INVALID_HANDLE_VALUE) { + FindClose(handle); + *((FILETIME *)(dst) + dwChar * 2 + 1) = FindFileData.ftLastWriteTime; + mpqapi_reg_store_modification_time(dst, 160); + } } +} -#ifdef SPAWN - return SRegSaveData(APP_NAME, "Audio Playback ", 0, (BYTE *)pbData, dwLen); -#else - return SRegSaveData(APP_NAME, "Video Player ", 0, (BYTE *)pbData, dwLen); -#endif +void mpqapi_store_creation_time(const char *pszArchive, DWORD dwChar) +{ + HANDLE handle; + struct _WIN32_FIND_DATAA FindFileData; + char dst[160]; + + if (gbMaxPlayers != 1) { + mpqapi_reg_load_modification_time(dst, 160); + handle = FindFirstFile(pszArchive, &FindFileData); + if (handle != INVALID_HANDLE_VALUE) { + FindClose(handle); + *((FILETIME *)(dst) + dwChar * 2) = FindFileData.ftCreationTime; + mpqapi_reg_store_modification_time(dst, 160); + } + } } -void mpqapi_remove_hash_entry(const char *pszName) +static _BLOCKENTRY *mpqapi_new_block(int *block_index) { - _HASHENTRY *pHashTbl; _BLOCKENTRY *blockEntry; - int hIdx, block_offset, block_size; + DWORD i; - hIdx = FetchHandle(pszName); - if (hIdx != -1) { - pHashTbl = &sgpHashTbl[hIdx]; - blockEntry = &sgpBlockTbl[pHashTbl->block]; - pHashTbl->block = -2; - block_offset = blockEntry->offset; - block_size = blockEntry->sizealloc; - memset(blockEntry, 0, sizeof(*blockEntry)); - mpqapi_alloc_block(block_offset, block_size); - save_archive_modified = TRUE; + blockEntry = sgpBlockTbl; + + i = 0; + while (blockEntry->offset || blockEntry->sizealloc || blockEntry->flags || blockEntry->sizefile) { + i++; + blockEntry++; + if (i >= 2048) { + app_fatal("Out of free block entries"); + return NULL; + } } + if (block_index) + *block_index = i; + + return blockEntry; } -void mpqapi_alloc_block(int block_offset, int block_size) +static void mpqapi_alloc_block(int block_offset, int block_size) { _BLOCKENTRY *block; int i; @@ -196,34 +216,38 @@ void mpqapi_alloc_block(int block_offset, int block_size) } } -_BLOCKENTRY *mpqapi_new_block(int *block_index) +static int mpqapi_find_free_block(int size, int *block_size) { - _BLOCKENTRY *blockEntry; - DWORD i; - - blockEntry = sgpBlockTbl; + _BLOCKENTRY *pBlockTbl; + int i, result; - i = 0; - while (blockEntry->offset || blockEntry->sizealloc || blockEntry->flags || blockEntry->sizefile) { - i++; - blockEntry++; - if (i >= 2048) { - app_fatal("Out of free block entries"); - return NULL; + pBlockTbl = sgpBlockTbl; + i = 2048; + while (1) { + i--; + if (pBlockTbl->offset && !pBlockTbl->flags && !pBlockTbl->sizefile && (DWORD)pBlockTbl->sizealloc >= size) + break; + pBlockTbl++; + if (!i) { + *block_size = size; + result = sgdwMpqOffset; + sgdwMpqOffset += size; + return result; } } - if (block_index) - *block_index = i; - return blockEntry; -} + result = pBlockTbl->offset; + *block_size = size; + pBlockTbl->offset += size; + pBlockTbl->sizealloc -= size; -int FetchHandle(const char *pszName) -{ - return mpqapi_get_hash_index(Hash(pszName, 0), Hash(pszName, 1), Hash(pszName, 2), 0); + if (!pBlockTbl->sizealloc) + memset(pBlockTbl, 0, sizeof(*pBlockTbl)); + + return result; } -int mpqapi_get_hash_index(short index, int hash_a, int hash_b, int locale) +static int mpqapi_get_hash_index(short index, int hash_a, int hash_b, int locale) { int idx, i; @@ -238,32 +262,141 @@ int mpqapi_get_hash_index(short index, int hash_a, int hash_b, int locale) return -1; } -void mpqapi_remove_hash_entries(BOOL(__stdcall *fnGetName)(DWORD, char *)) +static BOOL WriteMPQHeader() { - DWORD dwIndex, i; - char pszFileName[MAX_PATH]; + _FILEHEADER fhdr; + DWORD NumberOfBytesWritten; - dwIndex = 1; - for (i = fnGetName(0, pszFileName); i; i = fnGetName(dwIndex++, pszFileName)) { - mpqapi_remove_hash_entry(pszFileName); - } + memset(&fhdr, 0, sizeof(fhdr)); + fhdr.signature = '\x1AQPM'; + fhdr.headersize = 32; + fhdr.filesize = GetFileSize(sghArchive, 0); + fhdr.version = 0; + fhdr.sectorsizeid = 3; + fhdr.hashoffset = 32872; + fhdr.blockoffset = 104; + fhdr.hashcount = 2048; + fhdr.blockcount = 2048; + + if (SetFilePointer(sghArchive, 0, NULL, FILE_BEGIN) == -1) + return FALSE; + if (!WriteFile(sghArchive, &fhdr, sizeof(fhdr), &NumberOfBytesWritten, 0)) + return FALSE; + + return NumberOfBytesWritten == 104; } -BOOL mpqapi_write_file(const char *pszName, const BYTE *pbData, DWORD dwLen) +static BOOL mpqapi_write_block_table() { - _BLOCKENTRY *blockEntry; + BOOL success; + DWORD NumberOfBytesWritten; - save_archive_modified = TRUE; - mpqapi_remove_hash_entry(pszName); - blockEntry = mpqapi_add_file(pszName, 0, 0); - if (!mpqapi_write_file_contents(pszName, pbData, dwLen, blockEntry)) { - mpqapi_remove_hash_entry(pszName); + if (SetFilePointer(sghArchive, 104, NULL, FILE_BEGIN) == -1) + return FALSE; + + Encrypt((DWORD *)sgpBlockTbl, 0x8000, Hash("(block table)", 3)); + success = WriteFile(sghArchive, sgpBlockTbl, 0x8000, &NumberOfBytesWritten, 0); + Decrypt((DWORD *)sgpBlockTbl, 0x8000, Hash("(block table)", 3)); + return success && NumberOfBytesWritten == 0x8000; +} + +static BOOL mpqapi_write_hash_table() +{ + BOOL success; + DWORD NumberOfBytesWritten; + + if (SetFilePointer(sghArchive, 32872, NULL, FILE_BEGIN) == -1) return FALSE; + + Encrypt((DWORD *)sgpHashTbl, 0x8000, Hash("(hash table)", 3)); + success = WriteFile(sghArchive, sgpHashTbl, 0x8000, &NumberOfBytesWritten, 0); + Decrypt((DWORD *)sgpHashTbl, 0x8000, Hash("(hash table)", 3)); + return success && NumberOfBytesWritten == 0x8000; +} + +static BOOL mpqapi_can_seek() +{ + if (SetFilePointer(sghArchive, sgdwMpqOffset, NULL, FILE_BEGIN) == -1) + return FALSE; + return SetEndOfFile(sghArchive); +} + +static BOOL ParseMPQHeader(_FILEHEADER *pHdr, DWORD *pdwNextFileStart) +{ + DWORD size; + DWORD NumberOfBytesRead; + + size = GetFileSize(sghArchive, 0); + *pdwNextFileStart = size; + + if (size == -1 + || size < sizeof(*pHdr) + || !ReadFile(sghArchive, pHdr, sizeof(*pHdr), &NumberOfBytesRead, NULL) + || NumberOfBytesRead != 104 + || pHdr->signature != '\x1AQPM' + || pHdr->headersize != 32 + || pHdr->version > 0 + || pHdr->sectorsizeid != 3 + || pHdr->filesize != size + || pHdr->hashoffset != 32872 + || pHdr->blockoffset != 104 + || pHdr->hashcount != 2048 + || pHdr->blockcount != 2048) { + + if (SetFilePointer(sghArchive, 0, NULL, FILE_BEGIN) == -1) + return FALSE; + if (!SetEndOfFile(sghArchive)) + return FALSE; + + memset(pHdr, 0, sizeof(*pHdr)); + pHdr->signature = '\x1AQPM'; + pHdr->headersize = 32; + pHdr->sectorsizeid = 3; + pHdr->version = 0; + *pdwNextFileStart = 0x10068; + save_archive_modified = TRUE; + save_archive_open = TRUE; } + return TRUE; } -_BLOCKENTRY *mpqapi_add_file(const char *pszName, _BLOCKENTRY *pBlk, int block_index) +static int FetchHandle(const char *pszName) +{ + return mpqapi_get_hash_index(Hash(pszName, 0), Hash(pszName, 1), Hash(pszName, 2), 0); +} + +void mpqapi_remove_hash_entry(const char *pszName) +{ + _HASHENTRY *pHashTbl; + _BLOCKENTRY *blockEntry; + int hIdx, block_offset, block_size; + + hIdx = FetchHandle(pszName); + if (hIdx != -1) { + pHashTbl = &sgpHashTbl[hIdx]; + blockEntry = &sgpBlockTbl[pHashTbl->block]; + pHashTbl->block = -2; + block_offset = blockEntry->offset; + block_size = blockEntry->sizealloc; + memset(blockEntry, 0, sizeof(*blockEntry)); + mpqapi_alloc_block(block_offset, block_size); + save_archive_modified = TRUE; + } +} + +void mpqapi_remove_hash_entries(BOOL(__stdcall *fnGetName)(DWORD, char *)) +{ + DWORD dwIndex, i; + char pszFileName[MAX_PATH]; + + dwIndex = 1; + for (i = fnGetName(0, pszFileName); i; i = fnGetName(dwIndex++, pszFileName)) { + mpqapi_remove_hash_entry(pszFileName); + } +} + +static _BLOCKENTRY *mpqapi_add_file(const char *pszName, _BLOCKENTRY *pBlk, int block_index) { DWORD h1, h2, h3; int i, hIdx; @@ -293,7 +426,7 @@ _BLOCKENTRY *mpqapi_add_file(const char *pszName, _BLOCKENTRY *pBlk, int block_i return pBlk; } -BOOL mpqapi_write_file_contents(const char *pszName, const BYTE *pbData, DWORD dwLen, _BLOCKENTRY *pBlk) +static BOOL mpqapi_write_file_contents(const char *pszName, const BYTE *pbData, DWORD dwLen, _BLOCKENTRY *pBlk) { DWORD *sectoroffsettable; DWORD destsize, num_bytes, block_size, nNumberOfBytesToWrite; @@ -379,35 +512,18 @@ on_error: return FALSE; } -int mpqapi_find_free_block(int size, int *block_size) +BOOL mpqapi_write_file(const char *pszName, const BYTE *pbData, DWORD dwLen) { - _BLOCKENTRY *pBlockTbl; - int i, result; + _BLOCKENTRY *blockEntry; - pBlockTbl = sgpBlockTbl; - i = 2048; - while (1) { - i--; - if (pBlockTbl->offset && !pBlockTbl->flags && !pBlockTbl->sizefile && (DWORD)pBlockTbl->sizealloc >= size) - break; - pBlockTbl++; - if (!i) { - *block_size = size; - result = sgdwMpqOffset; - sgdwMpqOffset += size; - return result; - } + save_archive_modified = TRUE; + mpqapi_remove_hash_entry(pszName); + blockEntry = mpqapi_add_file(pszName, 0, 0); + if (!mpqapi_write_file_contents(pszName, pbData, dwLen, blockEntry)) { + mpqapi_remove_hash_entry(pszName); + return FALSE; } - - result = pBlockTbl->offset; - *block_size = size; - pBlockTbl->offset += size; - pBlockTbl->sizealloc -= size; - - if (!pBlockTbl->sizealloc) - memset(pBlockTbl, 0, sizeof(*pBlockTbl)); - - return result; + return TRUE; } void mpqapi_rename(char *pszOld, char *pszNew) @@ -432,6 +548,26 @@ BOOL mpqapi_has_file(const char *pszName) return FetchHandle(pszName) != -1; } +static void CloseMPQ(const char *pszArchive, BOOL bFree, DWORD dwChar) +{ + if (bFree) { + MemFreeDbg(sgpBlockTbl); + MemFreeDbg(sgpHashTbl); + } + if (sghArchive != INVALID_HANDLE_VALUE) { + CloseHandle(sghArchive); + sghArchive = INVALID_HANDLE_VALUE; + } + if (save_archive_modified) { + save_archive_modified = FALSE; + mpqapi_store_modified_time(pszArchive, dwChar); + } + if (save_archive_open) { + save_archive_open = FALSE; + mpqapi_store_creation_time(pszArchive, dwChar); + } +} + BOOL OpenMPQ(const char *pszArchive, BOOL hidden, DWORD dwChar) { DWORD dwFlagsAndAttributes; @@ -486,83 +622,6 @@ on_error: return FALSE; } -BOOL ParseMPQHeader(_FILEHEADER *pHdr, DWORD *pdwNextFileStart) -{ - DWORD size; - DWORD NumberOfBytesRead; - - size = GetFileSize(sghArchive, 0); - *pdwNextFileStart = size; - - if (size == -1 - || size < sizeof(*pHdr) - || !ReadFile(sghArchive, pHdr, sizeof(*pHdr), &NumberOfBytesRead, NULL) - || NumberOfBytesRead != 104 - || pHdr->signature != '\x1AQPM' - || pHdr->headersize != 32 - || pHdr->version > 0 - || pHdr->sectorsizeid != 3 - || pHdr->filesize != size - || pHdr->hashoffset != 32872 - || pHdr->blockoffset != 104 - || pHdr->hashcount != 2048 - || pHdr->blockcount != 2048) { - - if (SetFilePointer(sghArchive, 0, NULL, FILE_BEGIN) == -1) - return FALSE; - if (!SetEndOfFile(sghArchive)) - return FALSE; - - memset(pHdr, 0, sizeof(*pHdr)); - pHdr->signature = '\x1AQPM'; - pHdr->headersize = 32; - pHdr->sectorsizeid = 3; - pHdr->version = 0; - *pdwNextFileStart = 0x10068; - save_archive_modified = TRUE; - save_archive_open = TRUE; - } - - return TRUE; -} - -void CloseMPQ(const char *pszArchive, BOOL bFree, DWORD dwChar) -{ - if (bFree) { - MemFreeDbg(sgpBlockTbl); - MemFreeDbg(sgpHashTbl); - } - if (sghArchive != INVALID_HANDLE_VALUE) { - CloseHandle(sghArchive); - sghArchive = INVALID_HANDLE_VALUE; - } - if (save_archive_modified) { - save_archive_modified = FALSE; - mpqapi_store_modified_time(pszArchive, dwChar); - } - if (save_archive_open) { - save_archive_open = FALSE; - mpqapi_store_creation_time(pszArchive, dwChar); - } -} - -void mpqapi_store_modified_time(const char *pszArchive, DWORD dwChar) -{ - HANDLE handle; - struct _WIN32_FIND_DATAA FindFileData; - char dst[160]; - - if (gbMaxPlayers != 1) { - mpqapi_reg_load_modification_time(dst, 160); - handle = FindFirstFile(pszArchive, &FindFileData); - if (handle != INVALID_HANDLE_VALUE) { - FindClose(handle); - *((FILETIME *)(dst) + dwChar * 2 + 1) = FindFileData.ftLastWriteTime; - mpqapi_reg_store_modification_time(dst, 160); - } - } -} - BOOL mpqapi_flush_and_close(const char *pszArchive, BOOL bFree, DWORD dwChar) { BOOL ret = FALSE; @@ -582,62 +641,3 @@ BOOL mpqapi_flush_and_close(const char *pszArchive, BOOL bFree, DWORD dwChar) CloseMPQ(pszArchive, bFree, dwChar); return ret; } - -BOOL WriteMPQHeader() -{ - _FILEHEADER fhdr; - DWORD NumberOfBytesWritten; - - memset(&fhdr, 0, sizeof(fhdr)); - fhdr.signature = '\x1AQPM'; - fhdr.headersize = 32; - fhdr.filesize = GetFileSize(sghArchive, 0); - fhdr.version = 0; - fhdr.sectorsizeid = 3; - fhdr.hashoffset = 32872; - fhdr.blockoffset = 104; - fhdr.hashcount = 2048; - fhdr.blockcount = 2048; - - if (SetFilePointer(sghArchive, 0, NULL, FILE_BEGIN) == -1) - return FALSE; - if (!WriteFile(sghArchive, &fhdr, sizeof(fhdr), &NumberOfBytesWritten, 0)) - return FALSE; - - return NumberOfBytesWritten == 104; -} - -BOOL mpqapi_write_block_table() -{ - BOOL success; - DWORD NumberOfBytesWritten; - - if (SetFilePointer(sghArchive, 104, NULL, FILE_BEGIN) == -1) - return FALSE; - - Encrypt((DWORD *)sgpBlockTbl, 0x8000, Hash("(block table)", 3)); - success = WriteFile(sghArchive, sgpBlockTbl, 0x8000, &NumberOfBytesWritten, 0); - Decrypt((DWORD *)sgpBlockTbl, 0x8000, Hash("(block table)", 3)); - return success && NumberOfBytesWritten == 0x8000; -} - -BOOL mpqapi_write_hash_table() -{ - BOOL success; - DWORD NumberOfBytesWritten; - - if (SetFilePointer(sghArchive, 32872, NULL, FILE_BEGIN) == -1) - return FALSE; - - Encrypt((DWORD *)sgpHashTbl, 0x8000, Hash("(hash table)", 3)); - success = WriteFile(sghArchive, sgpHashTbl, 0x8000, &NumberOfBytesWritten, 0); - Decrypt((DWORD *)sgpHashTbl, 0x8000, Hash("(hash table)", 3)); - return success && NumberOfBytesWritten == 0x8000; -} - -BOOL mpqapi_can_seek() -{ - if (SetFilePointer(sghArchive, sgdwMpqOffset, NULL, FILE_BEGIN) == -1) - return FALSE; - return SetEndOfFile(sghArchive); -} diff --git a/Source/mpqapi.h b/Source/mpqapi.h index 3f9e2f33a..4008b5d7d 100644 --- a/Source/mpqapi.h +++ b/Source/mpqapi.h @@ -6,40 +6,13 @@ #ifndef __MPQAPI_H__ #define __MPQAPI_H__ -extern BYTE mpq_buf[4096]; -extern BOOL save_archive_modified; -extern BOOLEAN save_archive_open; - -BOOL mpqapi_set_hidden(const char *pszArchive, BOOL hidden); -void mpqapi_store_creation_time(const char *pszArchive, DWORD dwChar); -BOOL mpqapi_reg_load_modification_time(char *dst, int size); -void mpqapi_xor_buf(char *pbData); void mpqapi_store_default_time(DWORD dwChar); -BOOLEAN mpqapi_reg_store_modification_time(char *pbData, DWORD dwLen); void mpqapi_remove_hash_entry(const char *pszName); -void mpqapi_alloc_block(int block_offset, int block_size); -_BLOCKENTRY *mpqapi_new_block(int *block_index); -int FetchHandle(const char *pszName); -int mpqapi_get_hash_index(short index, int hash_a, int hash_b, int locale); void mpqapi_remove_hash_entries(BOOL(__stdcall *fnGetName)(DWORD, char *)); BOOL mpqapi_write_file(const char *pszName, const BYTE *pbData, DWORD dwLen); -_BLOCKENTRY *mpqapi_add_file(const char *pszName, _BLOCKENTRY *pBlk, int block_index); -BOOL mpqapi_write_file_contents(const char *pszName, const BYTE *pbData, DWORD dwLen, _BLOCKENTRY *pBlk); -int mpqapi_find_free_block(int size, int *block_size); void mpqapi_rename(char *pszOld, char *pszNew); BOOL mpqapi_has_file(const char *pszName); BOOL OpenMPQ(const char *pszArchive, BOOL hidden, DWORD dwChar); -BOOL ParseMPQHeader(_FILEHEADER *pHdr, DWORD *pdwNextFileStart); -void CloseMPQ(const char *pszArchive, BOOL bFree, DWORD dwChar); -void mpqapi_store_modified_time(const char *pszArchive, DWORD dwChar); BOOL mpqapi_flush_and_close(const char *pszArchive, BOOL bFree, DWORD dwChar); -BOOL WriteMPQHeader(); -BOOL mpqapi_write_block_table(); -BOOL mpqapi_write_hash_table(); -BOOL mpqapi_can_seek(); - -/* rdata */ - -/* data */ #endif /* __MPQAPI_H__ */