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

Update vending/buyingstore packets to use structs #3131

Merged
merged 10 commits into from
Jun 2, 2022
4 changes: 4 additions & 0 deletions src/common/HPMDataCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ HPExport const struct s_HPMDataCheck HPMDataCheck[] = {
#define MAP_NPC_H
#endif // MAP_NPC_H
#ifdef MAP_PACKETS_STRUCT_H
{ "CZ_PURCHASE_ITEM_FROMMC", sizeof(struct CZ_PURCHASE_ITEM_FROMMC), SERVER_TYPE_MAP },
{ "EQUIPITEM_INFO", sizeof(struct EQUIPITEM_INFO), SERVER_TYPE_MAP },
{ "EQUIPSLOTINFO", sizeof(struct EQUIPSLOTINFO), SERVER_TYPE_MAP },
{ "GUILD_MEMBER_INFO", sizeof(struct GUILD_MEMBER_INFO), SERVER_TYPE_MAP },
Expand All @@ -657,6 +658,8 @@ HPExport const struct s_HPMDataCheck HPMDataCheck[] = {
{ "PACKET_CZ_PC_BUY_CASH_POINT_ITEM", sizeof(struct PACKET_CZ_PC_BUY_CASH_POINT_ITEM), SERVER_TYPE_MAP },
{ "PACKET_CZ_PC_BUY_CASH_POINT_ITEM_sub", sizeof(struct PACKET_CZ_PC_BUY_CASH_POINT_ITEM_sub), SERVER_TYPE_MAP },
{ "PACKET_CZ_PC_PURCHASE_ITEMLIST", sizeof(struct PACKET_CZ_PC_PURCHASE_ITEMLIST), SERVER_TYPE_MAP },
{ "PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC", sizeof(struct PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC), SERVER_TYPE_MAP },
{ "PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC2", sizeof(struct PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC2), SERVER_TYPE_MAP },
{ "PACKET_CZ_PC_PURCHASE_ITEMLIST_sub", sizeof(struct PACKET_CZ_PC_PURCHASE_ITEMLIST_sub), SERVER_TYPE_MAP },
{ "PACKET_CZ_PET_EVOLUTION", sizeof(struct PACKET_CZ_PET_EVOLUTION), SERVER_TYPE_MAP },
{ "PACKET_CZ_PRIVATE_AIRSHIP_REQUEST", sizeof(struct PACKET_CZ_PRIVATE_AIRSHIP_REQUEST), SERVER_TYPE_MAP },
Expand Down Expand Up @@ -798,6 +801,7 @@ HPExport const struct s_HPMDataCheck HPMDataCheck[] = {
{ "PACKET_ZC_STATE_CHANGE", sizeof(struct PACKET_ZC_STATE_CHANGE), SERVER_TYPE_MAP },
{ "PACKET_ZC_STATUS_CHANGE", sizeof(struct PACKET_ZC_STATUS_CHANGE), SERVER_TYPE_MAP },
{ "PACKET_ZC_STATUS_CHANGE_ACK", sizeof(struct PACKET_ZC_STATUS_CHANGE_ACK), SERVER_TYPE_MAP },
{ "PACKET_ZC_STORE_ENTRY", sizeof(struct PACKET_ZC_STORE_ENTRY), SERVER_TYPE_MAP },
{ "PACKET_ZC_STYLE_CHANGE_RES", sizeof(struct PACKET_ZC_STYLE_CHANGE_RES), SERVER_TYPE_MAP },
{ "PACKET_ZC_TALKBOX_CHATCONTENTS", sizeof(struct PACKET_ZC_TALKBOX_CHATCONTENTS), SERVER_TYPE_MAP },
{ "PACKET_ZC_TRYCAPTURE_MONSTER", sizeof(struct PACKET_ZC_TRYCAPTURE_MONSTER), SERVER_TYPE_MAP },
Expand Down
4 changes: 2 additions & 2 deletions src/map/buyingstore.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ static void buyingstore_create(struct map_session_data *sd, int zenylimit, unsig
sd->buyingstore.slots = i; // store actual amount of items
safestrncpy(sd->message, storename, sizeof(sd->message));
clif->buyingstore_myitemlist(sd);
clif->buyingstore_entry(sd);
clif->buyingstore_entry(&sd->bl, sd->message);
}

static void buyingstore_close(struct map_session_data *sd)
Expand All @@ -210,7 +210,7 @@ static void buyingstore_close(struct map_session_data *sd)
memset(&sd->buyingstore, 0, sizeof(sd->buyingstore));

// notify other players
clif->buyingstore_disappear_entry(sd);
clif->buyingstore_disappear_entry(&sd->bl);
}
}

Expand Down
115 changes: 52 additions & 63 deletions src/map/clif.c
Original file line number Diff line number Diff line change
Expand Up @@ -4938,7 +4938,7 @@ static void clif_getareachar_pc(struct map_session_data *sd, struct map_session_
} else if( dstsd->state.vending )
clif->showvendingboard(&dstsd->bl,dstsd->message,sd->fd);
else if( dstsd->state.buyingstore )
clif->buyingstore_entry_single(sd, dstsd);
clif->buyingstore_entry_single(&dstsd->bl, dstsd->message, sd->fd);

if(dstsd->spiritball > 0)
clif->spiritball_single(sd->fd, dstsd);
Expand Down Expand Up @@ -5474,7 +5474,7 @@ static int clif_outsight(struct block_list *bl, va_list ap)
if( sd->state.vending )
clif->closevendingboard(bl,tsd->fd);
if( sd->state.buyingstore )
clif->buyingstore_disappear_entry_single(tsd, sd);
clif->buyingstore_disappear_entry_single(bl, tsd->fd);
break;
case BL_ITEM:
clif->clearflooritem(BL_UCAST(BL_ITEM, bl), tsd->fd);
Expand Down Expand Up @@ -7105,20 +7105,19 @@ static void clif_openvendingreq(struct map_session_data *sd, int num)
/// 0131 <owner id>.L <message>.80B
static void clif_showvendingboard(struct block_list *bl, const char *message, int fd)
{
unsigned char buf[128];

nullpo_retv(bl);

WBUFW(buf,0) = 0x131;
WBUFL(buf,2) = bl->id;
safestrncpy(WBUFP(buf,6), message, 80);

if( fd ) {
WFIFOHEAD(fd,packet_len(0x131));
memcpy(WFIFOP(fd,0),buf,packet_len(0x131));
WFIFOSET(fd,packet_len(0x131));
nullpo_retv(message);

struct PACKET_ZC_STORE_ENTRY p = { 0 };
p.packetType = HEADER_ZC_STORE_ENTRY;
p.makerAID = bl->id;
safestrncpy(p.storeName, message, MESSAGE_SIZE);
if (fd != 0) {
WFIFOHEAD(fd, sizeof(struct PACKET_ZC_STORE_ENTRY));
memcpy(WFIFOP(fd, 0), &p, sizeof(struct PACKET_ZC_STORE_ENTRY));
WFIFOSET(fd, sizeof(struct PACKET_ZC_STORE_ENTRY));
} else {
clif->send(buf,packet_len(0x131),bl,AREA_WOS);
clif->send(&p, sizeof(struct PACKET_ZC_STORE_ENTRY), bl, AREA_WOS);
}
}

Expand Down Expand Up @@ -12642,7 +12641,7 @@ static void clif_parse_NpcClicked(int fd, struct map_session_data *sd)
{
struct block_list *bl;

struct PACKET_CZ_CONTACTNPC *packet = RFIFOP(fd, 0);
const struct PACKET_CZ_CONTACTNPC *packet = RFIFOP(fd, 0);

if( pc_isdead(sd) ) {
clif->clearunit_area(&sd->bl,CLR_DEAD);
Expand Down Expand Up @@ -15222,17 +15221,14 @@ static void clif_parse_PurchaseReq(int fd, struct map_session_data *sd)
if (sd->state.trading || pc_isdead(sd) || pc_isvending(sd))
return;

int len = (int)RFIFOW(fd, 2) - 8;
int id;
const uint8 *data;
const struct PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC *p = RP2PTR(fd);
const int len = p->packetLength - sizeof(struct PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC);

if (len < 1)
return;

id = RFIFOL(fd, 4);
data = RFIFOP(fd, 8);

vending->purchase(sd, id, sd->vended_id, data, len/4);
int item_count = len / sizeof(struct CZ_PURCHASE_ITEM_FROMMC);
vending->purchase(sd, p->AID, sd->vended_id, p->list, item_count);

// whether it fails or not, the buy window is closed
sd->vended_id = 0;
Expand All @@ -15246,18 +15242,14 @@ static void clif_parse_PurchaseReq2(int fd, struct map_session_data *sd)
if (sd->state.trading || pc_isdead(sd) || pc_isvending(sd))
return;

int len = (int)RFIFOW(fd, 2) - 12;
int aid;
int uid;
const uint8 *data;
const struct PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC2 *p = RP2PTR(fd);
const int len = p->packetLength - sizeof(struct PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC2);

if (len < 1)
return;
aid = RFIFOL(fd, 4);
uid = RFIFOL(fd, 8);
data = RFIFOP(fd, 12);

vending->purchase(sd, aid, uid, data, len/4);
int item_count = len / sizeof(struct CZ_PURCHASE_ITEM_FROMMC);
vending->purchase(sd, p->AID, p->UniqueID, p->list, item_count);

// whether it fails or not, the buy window is closed
sd->vended_id = 0;
Expand Down Expand Up @@ -19958,31 +19950,31 @@ static void clif_buyingstore_myitemlist(struct map_session_data *sd)

/// Notifies clients in area of a buying store (ZC_BUYING_STORE_ENTRY).
/// 0814 <account id>.L <store name>.80B
static void clif_buyingstore_entry(struct map_session_data *sd)
static void clif_buyingstore_entry(struct block_list *bl, const char *message)
{
#if PACKETVER >= 20100420
uint8 buf[86];

nullpo_retv(sd);
WBUFW(buf,0) = 0x814;
WBUFL(buf,2) = sd->bl.id;
memcpy(WBUFP(buf,6), sd->message, MESSAGE_SIZE);
nullpo_retv(bl);
nullpo_retv(message);

clif->send(buf, packet_len(0x814), &sd->bl, AREA_WOS);
struct PACKET_ZC_BUYING_STORE_ENTRY p = { 0 };
p.packetType = HEADER_ZC_BUYING_STORE_ENTRY;
p.makerAID = bl->id;
safestrncpy(p.storeName, message, MESSAGE_SIZE);
clif->send(&p, sizeof(struct PACKET_ZC_BUYING_STORE_ENTRY), bl, AREA_WOS);
#endif
}
static void clif_buyingstore_entry_single(struct map_session_data *sd, struct map_session_data *pl_sd)
static void clif_buyingstore_entry_single(struct block_list *bl, const char *message, int fd)
{
#if PACKETVER >= 20100420
int fd;
nullpo_retv(bl);
nullpo_retv(message);

nullpo_retv(sd);
fd = sd->fd;
WFIFOHEAD(fd,packet_len(0x814));
WFIFOW(fd,0) = 0x814;
WFIFOL(fd,2) = pl_sd->bl.id;
memcpy(WFIFOP(fd,6), pl_sd->message, MESSAGE_SIZE);
WFIFOSET(fd,packet_len(0x814));
WFIFOHEAD(fd, sizeof(struct PACKET_ZC_BUYING_STORE_ENTRY));
struct PACKET_ZC_BUYING_STORE_ENTRY *p = WFIFOP(fd, 0);
p->packetType = HEADER_ZC_BUYING_STORE_ENTRY;
p->makerAID = bl->id;
safestrncpy(p->storeName, message, MESSAGE_SIZE);
WFIFOSET(fd, sizeof(struct PACKET_ZC_BUYING_STORE_ENTRY));
#endif
}

Expand All @@ -19996,31 +19988,28 @@ static void clif_parse_ReqCloseBuyingStore(int fd, struct map_session_data *sd)

/// Notifies clients in area that a buying store was closed (ZC_DISAPPEAR_BUYING_STORE_ENTRY).
/// 0816 <account id>.L
static void clif_buyingstore_disappear_entry(struct map_session_data *sd)
static void clif_buyingstore_disappear_entry(struct block_list *bl)
{
#if PACKETVER >= 20100309
uint8 buf[6];

nullpo_retv(sd);
WBUFW(buf,0) = 0x816;
WBUFL(buf,2) = sd->bl.id;
nullpo_retv(bl);

clif->send(buf, packet_len(0x816), &sd->bl, AREA_WOS);
struct PACKET_ZC_DISAPPEAR_BUYING_STORE_ENTRY p = { 0 };
p.packetType = HEADER_ZC_DISAPPEAR_BUYING_STORE_ENTRY;
p.makerAID = bl->id;
clif->send(&p, sizeof(struct PACKET_ZC_DISAPPEAR_BUYING_STORE_ENTRY), bl, AREA_WOS);
#endif
}

static void clif_buyingstore_disappear_entry_single(struct map_session_data *sd, struct map_session_data *pl_sd)
static void clif_buyingstore_disappear_entry_single(struct block_list *bl, int fd)
{
#if PACKETVER >= 20100309
int fd;
nullpo_retv(bl);

nullpo_retv(sd);
nullpo_retv(pl_sd);
fd = sd->fd;
WFIFOHEAD(fd,packet_len(0x816));
WFIFOW(fd,0) = 0x816;
WFIFOL(fd,2) = pl_sd->bl.id;
WFIFOSET(fd,packet_len(0x816));
WFIFOHEAD(fd, sizeof(struct PACKET_ZC_DISAPPEAR_BUYING_STORE_ENTRY));
struct PACKET_ZC_DISAPPEAR_BUYING_STORE_ENTRY *p = WFIFOP(fd, 0);
p->packetType = HEADER_ZC_DISAPPEAR_BUYING_STORE_ENTRY;
p->makerAID = bl->id;
WFIFOSET(fd, sizeof(struct PACKET_ZC_DISAPPEAR_BUYING_STORE_ENTRY));
#endif
}

Expand Down
8 changes: 4 additions & 4 deletions src/map/clif.h
Original file line number Diff line number Diff line change
Expand Up @@ -1414,10 +1414,10 @@ struct clif_interface {
void (*buyingstore_open) (struct map_session_data* sd);
void (*buyingstore_open_failed) (struct map_session_data* sd, unsigned short result, unsigned int weight);
void (*buyingstore_myitemlist) (struct map_session_data* sd);
void (*buyingstore_entry) (struct map_session_data* sd);
void (*buyingstore_entry_single) (struct map_session_data* sd, struct map_session_data* pl_sd);
void (*buyingstore_disappear_entry) (struct map_session_data* sd);
void (*buyingstore_disappear_entry_single) (struct map_session_data* sd, struct map_session_data* pl_sd);
void (*buyingstore_entry) (struct block_list *bl, const char *message);
void (*buyingstore_entry_single) (struct block_list *bl, const char *message, int fd);
void (*buyingstore_disappear_entry) (struct block_list *bl);
void (*buyingstore_disappear_entry_single) (struct block_list *bl, int fd);
void (*buyingstore_itemlist) (struct map_session_data* sd, struct map_session_data* pl_sd);
void (*buyingstore_trade_failed_buyer) (struct map_session_data* sd, short result);
void (*buyingstore_update_item) (struct map_session_data* sd, int nameid, unsigned short amount, uint32 char_id, int zeny);
Expand Down
47 changes: 46 additions & 1 deletion src/map/packets_struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -5502,7 +5502,6 @@ struct PACKET_CZ_APPROXIMATE_ACTOR {
DEFINE_PACKET_HEADER(CZ_APPROXIMATE_ACTOR, 0x0bb0)
#endif // PACKETVER_MAIN_NUM >= 20220216


struct PACKET_CZ_CONTACTNPC {
int16 PacketType;
uint32 AID;
Expand Down Expand Up @@ -5668,6 +5667,52 @@ struct PACKET_ZC_SOUND {
} __attribute__((packed));
DEFINE_PACKET_HEADER(ZC_SOUND, 0x01d3)

#if PACKETVER >= 20100420
struct PACKET_ZC_BUYING_STORE_ENTRY {
int16 packetType;
uint32 makerAID;
char storeName[MESSAGE_SIZE];
} __attribute__((packed));
DEFINE_PACKET_HEADER(ZC_BUYING_STORE_ENTRY, 0x0814);
#endif

struct PACKET_ZC_STORE_ENTRY {
int16 packetType;
uint32 makerAID;
char storeName[MESSAGE_SIZE];
} __attribute__((packed));
DEFINE_PACKET_HEADER(ZC_STORE_ENTRY, 0x0131);

struct CZ_PURCHASE_ITEM_FROMMC {
int16 count;
int16 index;
} __attribute__((packed));

struct PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC {
int16 packetType;
int16 packetLength;
uint32 AID;
struct CZ_PURCHASE_ITEM_FROMMC list[];
} __attribute__((packed));
DEFINE_PACKET_HEADER(CZ_PC_PURCHASE_ITEMLIST_FROMMC, 0x0134);

struct PACKET_CZ_PC_PURCHASE_ITEMLIST_FROMMC2 {
int16 packetType;
int16 packetLength;
uint32 AID;
uint32 UniqueID;
struct CZ_PURCHASE_ITEM_FROMMC list[];
} __attribute__((packed));
DEFINE_PACKET_HEADER(CZ_PC_PURCHASE_ITEMLIST_FROMMC2, 0x0801);

#if PACKETVER >= 20100309
struct PACKET_ZC_DISAPPEAR_BUYING_STORE_ENTRY {
int16 packetType;
uint32 makerAID;
} __attribute__((packed));
DEFINE_PACKET_HEADER(ZC_DISAPPEAR_BUYING_STORE_ENTRY, 0x0816);
#endif

#if !defined(sun) && (!defined(__NETBSD__) || __NetBSD_Version__ >= 600000000) // NetBSD 5 and Solaris don't like pragma pack but accept the packed attribute
#pragma pack(pop)
#endif // not NetBSD < 6 / Solaris
Expand Down
13 changes: 6 additions & 7 deletions src/map/vending.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "map/log.h"
#include "map/map.h"
#include "map/npc.h"
#include "map/packets_struct.h"
#include "map/path.h"
#include "map/pc.h"
#include "map/skill.h"
Expand Down Expand Up @@ -89,7 +90,7 @@ static void vending_vendinglistreq(struct map_session_data *sd, unsigned int id)
/*==========================================
* Purchase item(s) from a shop
*------------------------------------------*/
static void vending_purchasereq(struct map_session_data *sd, int aid, unsigned int uid, const uint8 *data, int count)
static void vending_purchasereq(struct map_session_data *sd, int aid, unsigned int uid, const struct CZ_PURCHASE_ITEM_FROMMC *data, int count)
{
int i, j, cursor, w, new_ = 0, blank, vend_list[MAX_VENDING];
int64 z;
Expand Down Expand Up @@ -123,9 +124,8 @@ static void vending_purchasereq(struct map_session_data *sd, int aid, unsigned i
z = 0; // zeny counter
w = 0; // weight counter
for (i = 0; i < count; i++) {
short amount = *(const uint16*)(data + 4*i + 0);
short idx = *(const uint16*)(data + 4*i + 2);
idx -= 2;
short amount = data[i].count;
short idx = data[i].index - 2;

if( amount <= 0 )
return;
Expand Down Expand Up @@ -189,9 +189,8 @@ static void vending_purchasereq(struct map_session_data *sd, int aid, unsigned i
pc->getzeny(vsd, (int)z, LOG_TYPE_VENDING, sd);

for (i = 0; i < count; i++) {
short amount = *(const uint16*)(data + 4*i + 0);
short idx = *(const uint16*)(data + 4*i + 2);
idx -= 2;
short amount = data[i].count;
short idx = data[i].index - 2;

// vending item
pc->additem(sd, &vsd->status.cart[idx], amount, LOG_TYPE_VENDING);
Expand Down
3 changes: 2 additions & 1 deletion src/map/vending.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
struct DBMap; // common/db.h
struct map_session_data;
struct s_search_store_search;
struct CZ_PURCHASE_ITEM_FROMMC;

struct s_vending {
short index; //cart index (return item data)
Expand All @@ -44,7 +45,7 @@ struct vending_interface {
void (*close) (struct map_session_data* sd);
void (*open) (struct map_session_data* sd, const char* message, const uint8* data, int count);
void (*list) (struct map_session_data* sd, unsigned int id);
void (*purchase) (struct map_session_data* sd, int aid, unsigned int uid, const uint8* data, int count);
void (*purchase) (struct map_session_data* sd, int aid, unsigned int uid, const struct CZ_PURCHASE_ITEM_FROMMC *data, int count);
bool (*search) (struct map_session_data* sd, int nameid);
bool (*searchall) (struct map_session_data* sd, const struct s_search_store_search* s);
};
Expand Down
Loading