Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More drive-by cleanup #122

Merged
merged 9 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions source/D2Common/include/D2Items.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,13 @@ int __fastcall ITEMS_CalculateAdditionalCostsForChargedSkills(D2UnitStrc* pUnit,
//D2Common.0x6FD9ACE0
void __fastcall ITEMS_CalculateAdditionalCostsForBonusStats(D2UnitStrc* pItem, int* pSellCost, int* pBuyCost, int* pRepCost, unsigned int nDivisor);
//D2Common.0x6FD9B1C0
int __fastcall ITEMS_CalculateTransactionCost(D2UnitStrc* pPlayer, D2UnitStrc* pItem, int nDifficulty, D2BitBufferStrc* pQuestFlags, int nVendorId, int nTransactionType);
int __fastcall ITEMS_CalculateTransactionCost(D2UnitStrc* pPlayer, D2UnitStrc* pItem, D2C_Difficulties nDifficulty, D2BitBufferStrc* pQuestFlags, int nVendorId, D2C_TransactionTypes nTransactionType);
//D2Common.0x6FD9CB50
void __fastcall ITEMS_CalculateAdditionalCostsForItemSkill(D2UnitStrc* pItem, int* pSellCost, int* pBuyCost, int* pRepCost, unsigned int nDivisor);
//D2Common.0x6FD9CDC0
int __fastcall ITEMS_CheckUnitFlagEx(D2UnitStrc* pUnit, int nFlag);
//D2Common.0x6FD9CDE0 (#10775)
D2COMMON_DLL_DECL int __stdcall ITEMS_GetTransactionCost(D2UnitStrc* pPlayer, D2UnitStrc* pItem, int nDifficulty, D2BitBufferStrc* pQuestFlags, int nVendorId, int nTransactionType);
D2COMMON_DLL_DECL int __stdcall ITEMS_GetTransactionCost(D2UnitStrc* pPlayer, D2UnitStrc* pItem, D2C_Difficulties nDifficulty, D2BitBufferStrc* pQuestFlags, int nVendorId, D2C_TransactionTypes nTransactionType);
//D2Common.0x6FD9CE10 (#10794)
D2COMMON_DLL_DECL int __stdcall ITEMS_GetMaxStack(int nItemId);
//D2Common.0x6FD9CE50 (#10795)
Expand Down Expand Up @@ -514,7 +514,7 @@ D2COMMON_DLL_DECL BOOL __stdcall ITEMS_IsPersonalizable(D2UnitStrc* pItem);
//D2Common.0x6FD9F260 (#10831)
D2COMMON_DLL_DECL BOOL __stdcall ITEMS_IsSocketable(D2UnitStrc* pItem);
//D2Common.0x6FD9F490 (#10877)
D2COMMON_DLL_DECL int __stdcall ITEMS_GetAllRepairCosts(D2GameStrc* pGame, D2UnitStrc* pUnit, int nNpcId, int nDifficulty, D2BitBufferStrc* pQuestFlags, void(__fastcall* pfCallback)(D2GameStrc*, D2UnitStrc*, D2UnitStrc*));
D2COMMON_DLL_DECL int __stdcall ITEMS_GetAllRepairCosts(D2GameStrc* pGame, D2UnitStrc* pUnit, int nNpcId, D2C_Difficulties nDifficulty, D2BitBufferStrc* pQuestFlags, void(__fastcall* pfCallback)(D2GameStrc*, D2UnitStrc*, D2UnitStrc*));
//D2Common.0x6FD9F720 (#10833)
D2COMMON_DLL_DECL BOOL __stdcall ITEMS_AreStackablesEqual(D2UnitStrc* pItem1, D2UnitStrc* pItem2);
//D2Common.0x6FD9FA70 (#10834)
Expand Down
24 changes: 12 additions & 12 deletions source/D2Common/src/Items/Items.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1958,7 +1958,7 @@ void __fastcall ITEMS_CalculateAdditionalCostsForBonusStats(D2UnitStrc* pItem, i

//TODO: Check Calculations for unnamed variables, contains some small (rounding?) errors
//D2Common.0x6FD9B1C0
int __fastcall ITEMS_CalculateTransactionCost(D2UnitStrc* pPlayer, D2UnitStrc* pItem, int nDifficulty, D2BitBufferStrc* pQuestFlags, int nVendorId, int nTransactionType)
int __fastcall ITEMS_CalculateTransactionCost(D2UnitStrc* pPlayer, D2UnitStrc* pItem, D2C_Difficulties nDifficulty, D2BitBufferStrc* pQuestFlags, int nVendorId, D2C_TransactionTypes nTransactionType)
{
D2UniqueItemsTxt* pUniqueItemsTxtRecord = NULL;
D2MagicAffixTxt* pMagicAffixTxtRecord = NULL;
Expand Down Expand Up @@ -2006,7 +2006,7 @@ int __fastcall ITEMS_CalculateTransactionCost(D2UnitStrc* pPlayer, D2UnitStrc* p

nLevel = STATLIST_UnitGetStatValue(pPlayer, STAT_LEVEL, 0);

if (nTransactionType == 3 && !ITEMS_IsRepairable(pItem))
if (nTransactionType == D2C_TransactionTypes::TRANSACTIONTYPE_REPAIR && !ITEMS_IsRepairable(pItem))
{
return 0;
}
Expand All @@ -2029,7 +2029,7 @@ int __fastcall ITEMS_CalculateTransactionCost(D2UnitStrc* pPlayer, D2UnitStrc* p
nReducePricePct = 99;
}

if (nTransactionType != 2)
if (nTransactionType != D2C_TransactionTypes::TRANSACTIONTYPE_GAMBLE)
{
nSellCost = 0;
nBuyCost = 0;
Expand Down Expand Up @@ -2338,7 +2338,7 @@ int __fastcall ITEMS_CalculateTransactionCost(D2UnitStrc* pPlayer, D2UnitStrc* p
}
}

if (nTransactionType == 1)
if (nTransactionType == D2C_TransactionTypes::TRANSACTIONTYPE_SELL)
{
if (pItemData->dwItemFlags & IFLAG_ETHEREAL && !pItemsTxtRecord->nNoDurability && pItemsTxtRecord->nDurability && STATLIST_GetMaxDurabilityFromUnit(pItem) && STATLIST_UnitGetStatValue(pItem, STAT_ITEM_INDESCTRUCTIBLE, 0) <= 0)
{
Expand All @@ -2348,7 +2348,7 @@ int __fastcall ITEMS_CalculateTransactionCost(D2UnitStrc* pPlayer, D2UnitStrc* p
}
}
}
else if (nTransactionType == 3)
else if (nTransactionType == D2C_TransactionTypes::TRANSACTIONTYPE_REPAIR)
{
pItemTypesTxtRecord = DATATBLS_GetItemTypesTxtRecord(pItemsTxtRecord->wType[0]);
if (!pItemTypesTxtRecord || !pItemTypesTxtRecord->wQuiver || !pItemTypesTxtRecord->nThrowable)
Expand Down Expand Up @@ -2483,7 +2483,7 @@ int __fastcall ITEMS_CalculateTransactionCost(D2UnitStrc* pPlayer, D2UnitStrc* p

nCost = nBuyCost;

if (nTransactionType == 3)
if (nTransactionType == D2C_TransactionTypes::TRANSACTIONTYPE_REPAIR)
{
if (!(pItemData->dwItemFlags & IFLAG_ETHEREAL))
{
Expand All @@ -2496,9 +2496,9 @@ int __fastcall ITEMS_CalculateTransactionCost(D2UnitStrc* pPlayer, D2UnitStrc* p
nCost = pNpcTxtRecord->nMaxBuy[nDifficulty];
}

if (nTransactionType != 1)
if (nTransactionType != D2C_TransactionTypes::TRANSACTIONTYPE_SELL)
{
if (nTransactionType == 3)
if (nTransactionType == D2C_TransactionTypes::TRANSACTIONTYPE_REPAIR)
{
nCost = nRepCost - DATATBLS_CalculatePercentage(nRepCost, nReducePricePct, 100);
}
Expand Down Expand Up @@ -2655,7 +2655,7 @@ int __fastcall ITEMS_CheckUnitFlagEx(D2UnitStrc* pUnit, int nFlag)
}

//D2Common.0x6FD9CDE0 (#10775)
int __stdcall ITEMS_GetTransactionCost(D2UnitStrc* pPlayer, D2UnitStrc* pItem, int nDifficulty, D2BitBufferStrc* pQuestFlags, int nVendorId, int nTransactionType)
int __stdcall ITEMS_GetTransactionCost(D2UnitStrc* pPlayer, D2UnitStrc* pItem, D2C_Difficulties nDifficulty, D2BitBufferStrc* pQuestFlags, int nVendorId, D2C_TransactionTypes nTransactionType)
{
return ITEMS_CalculateTransactionCost(pPlayer, pItem, nDifficulty, pQuestFlags, nVendorId, nTransactionType);
}
Expand Down Expand Up @@ -4232,7 +4232,7 @@ BOOL __stdcall ITEMS_IsSocketable(D2UnitStrc* pItem)
}

//D2Common.0x6FD9F490 (#10877)
int __stdcall ITEMS_GetAllRepairCosts(D2GameStrc* pGame, D2UnitStrc* pUnit, int nNpcId, int nDifficulty, D2BitBufferStrc* pQuestFlags, void(__fastcall* pfCallback)(D2GameStrc*, D2UnitStrc*, D2UnitStrc*))
int __stdcall ITEMS_GetAllRepairCosts(D2GameStrc* pGame, D2UnitStrc* pUnit, int nNpcId, D2C_Difficulties nDifficulty, D2BitBufferStrc* pQuestFlags, void(__fastcall* pfCallback)(D2GameStrc*, D2UnitStrc*, D2UnitStrc*))
{
D2ItemTypesTxt* pItemTypesTxtRecord = NULL;
D2ItemsTxt* pItemsTxtRecord = NULL;
Expand Down Expand Up @@ -4310,7 +4310,7 @@ int __stdcall ITEMS_GetAllRepairCosts(D2GameStrc* pGame, D2UnitStrc* pUnit, int
{
if (bCanBeRepaired)
{
nRepairCosts += ITEMS_CalculateTransactionCost(pUnit, pItem, nDifficulty, pQuestFlags, nNpcId, 3);
nRepairCosts += ITEMS_CalculateTransactionCost(pUnit, pItem, nDifficulty, pQuestFlags, nNpcId, D2C_TransactionTypes::TRANSACTIONTYPE_REPAIR);

if (pfCallback)
{
Expand All @@ -4328,7 +4328,7 @@ int __stdcall ITEMS_GetAllRepairCosts(D2GameStrc* pGame, D2UnitStrc* pUnit, int
{
if (bCanBeRepaired)
{
nRepairCosts += ITEMS_CalculateTransactionCost(pUnit, pItem, nDifficulty, pQuestFlags, nNpcId, 3);
nRepairCosts += ITEMS_CalculateTransactionCost(pUnit, pItem, nDifficulty, pQuestFlags, nNpcId, D2C_TransactionTypes::TRANSACTIONTYPE_REPAIR);

if (pfCallback)
{
Expand Down
14 changes: 7 additions & 7 deletions source/D2CommonDefinitions/include/D2Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ enum D2InteractStates
INTERACT_TRADE = 2, //your trading
};

enum D2C_TransactionTypes
{
TRANSACTIONTYPE_BUY = 0,
TRANSACTIONTYPE_SELL = 1,
TRANSACTIONTYPE_GAMBLE = 2,
TRANSACTIONTYPE_REPAIR = 3,
};

#ifndef CONSTANTS_LEVELS //Pseudo-Macro to group all constants

Expand Down Expand Up @@ -84,13 +91,6 @@ enum D2C_Acts

#endif // CONSTANTS_LEVELS



enum D2C_ClientStates
{
CLTSTATE_CONNECTED = 4
};

enum D2SaveCharType
{
CHARTYPE_HARDCORE = 0x24,
Expand Down
36 changes: 22 additions & 14 deletions source/D2CommonDefinitions/include/D2PacketDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,18 @@ struct D2GSPacketClt31 //size of 0x09

struct D2GSPacketClt32 //size of 0x11
{
uint8_t nHeader; //0x00
int32_t unk0x01; //0x01
int32_t unk0x05; //0x05
uint16_t unk0x09; //0x09
uint16_t unk0x0B; //0x0B
int32_t unk0x0D; //0x0D
uint8_t nHeader; //0x00
int32_t dwNpcGUID; //0x01
int32_t dwItemGUID; //0x05
uint16_t nTransactionType; //0x09
struct { //0x0B
uint8_t nItemMode : 8;
uint8_t : 7;
uint8_t bFill : 1;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit annoying that I can't do the following:

	struct {                                //0x0B
		uint16_t nItemMode : 15;
		uint8_t bFill : 1;
	};

seems the compiler insists on padding the uint16_t to 16 bits, at which point bFill causes an extra byte.

I don't think the 7 bits are actually used for anything, so I'm just dropping them on the floor.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because you are changing the type.
I'm guessing

struct {                                //0x0B
		uint16_t nItemMode : 15;
		uint16_t bFill : 1;
	};

would work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Learned something new. I could also now do something like:

	struct {
		D2C_TransactionTypes nTransactionType : 15;
		D2C_ItemModes nItemMode : 15;
		uint32_t bFill : 1;
	};

and then change signatures of D2GAME_NPC_BuyItemHandler_6FCC92A0 and below to use enum types that are bigger.
In reality the original functions have 2 byte types, but I think passing 4 bytes values might still work?
Perhaps it might not work if the values are passed on the stack depending on the calling convention?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the current version is good enough, putting enums in a bitfield will be ambigous/risky (different types might trigger new bitfield). I think it is good to have the uint16_t so that it is explicit that we are packing something unrelated into 16bits.

};
int32_t dwCost; //0x0D
};
static_assert(sizeof(D2GSPacketClt32) == 0x11, "size mismatch");

struct D2GSPacketClt33 //size of 0x11
{
Expand Down Expand Up @@ -383,8 +388,8 @@ struct D2GSPacketClt44 //size of 0x11
struct D2GSPacketClt45 //size of 0x09
{
uint8_t nHeader; //0x00
int32_t unk0x01; //0x01
int32_t unk0x05; //0x05
int32_t nPortalGUID; //0x01
int32_t nLevelId; //0x05
};

//TODO: 0x46 - 0x48
Expand All @@ -393,7 +398,7 @@ struct D2GSPacketClt49 //size of 0x09
{
uint8_t nHeader; //0x00
int32_t nWaypointGUID; //0x01
int32_t unk0x05; //0x05
int32_t nLevelId; //0x05
};

//TODO: 0x4A - 0x4E
Expand Down Expand Up @@ -433,7 +438,10 @@ struct D2GSPacketClt58 //size of 0x03
struct D2GSPacketClt59 //size of 0x11
{
uint8_t nHeader; //0x00
int32_t unk0x01[4]; //0x01
uint32_t nUnitType; //0x01
uint32_t dwUnitGUID; //0x05
uint32_t dwTargetX; //0x09
uint32_t dwTargetY; //0x0D
};

//TODO: 0x5A - 0x5C
Expand All @@ -450,7 +458,7 @@ struct D2GSPacketClt5E //size of 0x06
{
uint8_t nHeader; //0x00
uint8_t nType; //0x01
int32_t unk0x02; //0x02
int32_t dwPlayerGUID; //0x02
};

struct D2GSPacketClt5F
Expand Down Expand Up @@ -484,10 +492,10 @@ struct D2GSPacketClt66 //size of 0x2E
{
uint8_t nHeader; //0x00
char szGameName[16]; //0x01
uint8_t bSkipVerification; //0x11
uint8_t nGameType; //0x11
uint8_t nPlayerClass; //0x12
int8_t unk0x13; //0x13
int8_t unk0x14; //0x14
int8_t nTemplate; //0x13
int8_t nDifficulty ; //0x14
char szClientName[16]; //0x15
int16_t unk0x25; //0x25
uint32_t nGameFlags; //0x27
Expand Down
4 changes: 2 additions & 2 deletions source/D2Game/include/UNIT/SUnitNpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ D2UnitStrc* __fastcall D2GAME_MERCS_Create_6FCC8630(D2GameStrc* pGame, D2UnitStr
//D2Game.0x6FCC87C0
D2UnitStrc* __fastcall sub_6FCC87C0(D2GameStrc* pPlayer, D2UnitStrc* pUnit, D2UnitStrc* pItem, int32_t* a4);
//D2Game.0x6FCC88B0
int32_t __fastcall sub_6FCC88B0(D2GameStrc* pGame, D2UnitStrc* pPlayer, D2UnitStrc* pNpc, int32_t nItemGUID, int32_t a5, uint16_t nTab, int32_t nCost, int32_t a8);
int32_t __fastcall sub_6FCC88B0(D2GameStrc* pGame, D2UnitStrc* pPlayer, D2UnitStrc* pNpc, int32_t nItemGUID, int32_t nItemMode, uint16_t nTransactionType, int32_t nCost, int32_t bFill);
//D2Game.0x6FCC92A0
int32_t __fastcall D2GAME_NPC_BuyItemHandler_6FCC92A0(D2GameStrc* pGame, D2UnitStrc* pPlayer, int32_t nNpcUnitId, int32_t nItemId, int32_t a5, uint16_t nTab, int32_t nCost, int32_t a8);
int32_t __fastcall D2GAME_NPC_BuyItemHandler_6FCC92A0(D2GameStrc* pGame, D2UnitStrc* pPlayer, int32_t nNpcUnitId, int32_t nItemId, int32_t nItemMode, uint16_t nTransactionType, int32_t nCost, int32_t bFill);
//D2Game.0x6FCC9350
void __fastcall D2GAME_NPC_ResurrectMerc_6FCC9350(D2GameStrc* pGame, D2UnitStrc* pPlayer, int32_t nNpcUnitId);
//D2Game.0x6FCC9540
Expand Down
2 changes: 1 addition & 1 deletion source/D2Game/src/GAME/CCmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void __fastcall CCMD_ProcessClientSystemMessage(void* pData, int32_t nSize)
D2GSPacketClt66* pPacket66 = (D2GSPacketClt66*)pPacket;
if (GAME_VerifyCreateNewGame(nClientId, pPacket66))
{
GAME_SendGameInit(nClientId, pPacket66->szGameName, pPacket66->bSkipVerification, pPacket66->nPlayerClass, pPacket66->szClientName, pPacket66->unk0x25, pPacket66->nGameFlags, pPacket66->unk0x13, pPacket66->unk0x2B, pPacket66->unk0x2C, pPacket66->unk0x14, pPacket66->nLocale, 0, 0);
GAME_SendGameInit(nClientId, pPacket66->szGameName, pPacket66->nGameType, pPacket66->nPlayerClass, pPacket66->szClientName, pPacket66->unk0x25, pPacket66->nGameFlags, pPacket66->nTemplate, pPacket66->unk0x2B, pPacket66->unk0x2C, pPacket66->nDifficulty, pPacket66->nLocale, 0, 0);
}
else
{
Expand Down
12 changes: 6 additions & 6 deletions source/D2Game/src/GAME/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ void __fastcall GAME_ResolveGameNameConflict(D2GameStrc* pGameToSanitize, char*
//D2Game.0x6FC35CB0
int32_t __fastcall GAME_VerifyCreateNewGame(int32_t nClientId, D2GSPacketClt66* pPacket)
{
if (gpD2ServerCallbackFunctions_6FD45830 && pPacket->bSkipVerification)
if (gpD2ServerCallbackFunctions_6FD45830 && pPacket->nGameType)
{
return 0;
}
Expand Down Expand Up @@ -1386,7 +1386,7 @@ void __fastcall GAME_DisconnectClient(D2GameStrc* pGame, D2ClientStrc* pClient,
D2GSPacketSrv5A packet5A = {};
packet5A.nHeader = 0x5Au;
packet5A.nType = nEventType;
packet5A.nColor = 4;
packet5A.nColor = STRCOLOR_DARK_GOLD;
packet5A.dwParam = 0;
CLIENTS_CopyAccountNameToBuffer(pClient, &packet5A.szText[16]);
packet5A.szText[31] = '\0';
Expand Down Expand Up @@ -1508,7 +1508,7 @@ void __stdcall D2Game_10024_RemoveClientFromGame(int32_t nClientId)
D2GSPacketSrv5A packet5A = {};
packet5A.nHeader = 0x5Au;
packet5A.nType = EVENTTYPE_DISCONNECT;
packet5A.nColor = 4;
packet5A.nColor = STRCOLOR_DARK_GOLD;
packet5A.dwParam = 0;
packet5A.szText[16] = 0;
SStrCopy(packet5A.szText, CLIENTS_GetName(pClient), 16u);
Expand Down Expand Up @@ -1705,7 +1705,7 @@ void __fastcall GAME_EndGame(int32_t nClientId, int32_t a2)
D2GSPacketSrv5A packet5A = {};
packet5A.nHeader = 0x5Au;
packet5A.nType = EVENTTYPE_PLAYERLEFT;
packet5A.nColor = 4;
packet5A.nColor = STRCOLOR_DARK_GOLD;
packet5A.dwParam = 0;
CLIENTS_CopyAccountNameToBuffer(pClient, &packet5A.szText[16]);
packet5A.szText[31] = '\0';
Expand Down Expand Up @@ -2254,7 +2254,7 @@ void __fastcall D2GAME_UpdateAllClients_6FC389C0(D2GameStrc* pGame)
D2GSPacketSrv5A packet5A = {};
packet5A.nHeader = 0x5A;
packet5A.nType = EVENTTYPE_PLAYERJOIN;
packet5A.nColor = 4;
packet5A.nColor = STRCOLOR_DARK_GOLD;
packet5A.dwParam = 0;
packet5A.szText[16] = '\0';

Expand Down Expand Up @@ -3649,7 +3649,7 @@ void __stdcall D2Game_10021(int32_t a1, int32_t nPacketParam, const char* szMess
D2GSPacketSrv5A packet5A = {};
packet5A.nHeader = 0x5Au;
packet5A.nType = EVENTTYPE_REALMGOINGDOWN;
packet5A.nColor = 4;
packet5A.nColor = STRCOLOR_DARK_GOLD;
packet5A.dwParam = nPacketParam;
packet5A.szText[16] = 0;
sub_6FC84D40(pGame, &packet5A);
Expand Down
Loading