diff --git a/src/map/clif.cpp b/src/map/clif.cpp index d34fa7bcb3..cbc6d749ca 100644 --- a/src/map/clif.cpp +++ b/src/map/clif.cpp @@ -80,6 +80,9 @@ static struct eri *delay_clearunit_ers; struct s_packet_db packet_db[MAX_PACKET_DB + 1]; int packet_db_ack[MAX_ACK_FUNC + 1]; +// Reuseable global packet buffer to prevent too many allocations +// Take socket.cpp::socket_max_client_packet into consideration +static int8 packet_buffer[UINT16_MAX]; unsigned long color_table[COLOR_MAX]; #include "clif_obfuscation.hpp" @@ -20553,9 +20556,7 @@ void clif_navigateTo(struct map_session_data *sd, const char* mapname, uint16 x, /// Send hat effects to the client (ZC_HAT_EFFECT). /// 0A3B .W .L .B { .W } void clif_hat_effects( struct map_session_data* sd, struct block_list* bl, enum send_target target ){ -#if PACKETVER >= 20150513 - unsigned char* buf; - int len,i; +#if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO) struct map_session_data *tsd; struct block_list* tbl; @@ -20567,39 +20568,40 @@ void clif_hat_effects( struct map_session_data* sd, struct block_list* bl, enum tbl = bl; } - if( !tsd->hatEffectCount ) + nullpo_retv( tsd ); + + if( tsd->hatEffects.empty() ){ return; - - len = 9 + tsd->hatEffectCount * 2; - - buf = (unsigned char*)aMalloc( len ); - - WBUFW(buf,0) = 0xa3b; - WBUFW(buf,2) = len; - WBUFL(buf,4) = tsd->bl.id; - WBUFB(buf,8) = 1; - - for( i = 0; i < tsd->hatEffectCount; i++ ){ - WBUFW(buf,9+i*2) = tsd->hatEffectIDs[i]; } - clif_send(buf, len,tbl,target); + struct PACKET_ZC_HAT_EFFECT* p = (struct PACKET_ZC_HAT_EFFECT*)packet_buffer; - aFree(buf); + p->packetType = HEADER_ZC_HAT_EFFECT; + p->packetLength = (int16)( sizeof( struct PACKET_ZC_HAT_EFFECT ) + sizeof( int16 ) * tsd->hatEffects.size() ); + p->aid = tsd->bl.id; + p->status = 1; + + for( size_t i = 0; i < tsd->hatEffects.size(); i++ ){ + p->effects[i] = tsd->hatEffects[i]; + } + + clif_send( p, p->packetLength, tbl, target ); #endif } void clif_hat_effect_single( struct map_session_data* sd, uint16 effectId, bool enable ){ -#if PACKETVER >= 20150513 - unsigned char buf[13]; +#if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO) + nullpo_retv( sd ); - WBUFW(buf,0) = 0xa3b; - WBUFW(buf,2) = 13; - WBUFL(buf,4) = sd->bl.id; - WBUFB(buf,8) = enable; - WBUFL(buf,9) = effectId; + struct PACKET_ZC_HAT_EFFECT* p = (struct PACKET_ZC_HAT_EFFECT*)packet_buffer; - clif_send(buf,13,&sd->bl,AREA); + p->packetType = HEADER_ZC_HAT_EFFECT; + p->packetLength = (int16)( sizeof( struct PACKET_ZC_HAT_EFFECT ) + sizeof( int16 ) ); + p->aid = sd->bl.id; + p->status = enable; + p->effects[0] = effectId; + + clif_send( p, p->packetLength, &sd->bl, AREA ); #endif } diff --git a/src/map/clif_packetdb.hpp b/src/map/clif_packetdb.hpp index 234683b8d0..9609fbfa45 100644 --- a/src/map/clif_packetdb.hpp +++ b/src/map/clif_packetdb.hpp @@ -2248,10 +2248,12 @@ packet(0x0A2D,-1); // ZC_EQUIPWIN_MICROSCOPE_V6 #endif +#if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO) + packet( HEADER_ZC_HAT_EFFECT, -1 ); +#endif + // 2015-05-13aRagexe #if PACKETVER >= 20150513 - // New Packets - packet(0xA3B,-1); // ZC_HAT_EFFECT // RODEX Mail system packet(0x09E7,3); // ZC_NOTIFY_UNREADMAIL parseable_packet(0x09E8,11,clif_parse_Mail_refreshinbox,2,3); // CZ_OPEN_MAILBOX diff --git a/src/map/pc.cpp b/src/map/pc.cpp index f1556f38ef..bdf12eb33a 100755 --- a/src/map/pc.cpp +++ b/src/map/pc.cpp @@ -1663,9 +1663,8 @@ bool pc_authok(struct map_session_data *sd, uint32 login_id2, time_t expiration_ // Initialize BG queue sd->bg_queue_id = 0; -#if PACKETVER >= 20150513 - sd->hatEffectIDs = NULL; - sd->hatEffectCount = 0; +#if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO) + sd->hatEffects = {}; #endif sd->catch_target_class = PET_CATCH_FAIL; diff --git a/src/map/pc.hpp b/src/map/pc.hpp index 30324ddd2c..8a13caad06 100644 --- a/src/map/pc.hpp +++ b/src/map/pc.hpp @@ -787,9 +787,8 @@ struct map_session_data { short setlook_head_top, setlook_head_mid, setlook_head_bottom, setlook_robe; ///< Stores 'setlook' script command values. -#if PACKETVER >= 20150513 - uint32* hatEffectIDs; - uint8 hatEffectCount; +#if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO) + std::vector hatEffects; #endif struct{ diff --git a/src/map/script.cpp b/src/map/script.cpp index 890343932f..59e266aeb8 100644 --- a/src/map/script.cpp +++ b/src/map/script.cpp @@ -23191,47 +23191,34 @@ BUILDIN_FUNC(recalculatestat) { } BUILDIN_FUNC(hateffect){ -#if PACKETVER >= 20150513 +#if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO) struct map_session_data* sd; - bool enable; - int i, effectID; if( !script_rid2sd(sd) ) return SCRIPT_CMD_FAILURE; - effectID = script_getnum(st,2); - enable = script_getnum(st,3) ? true : false; + int16 effectID = script_getnum(st,2); + bool enable = script_getnum(st,3) ? true : false; if( effectID <= HAT_EF_MIN || effectID >= HAT_EF_MAX ){ ShowError( "buildin_hateffect: unsupported hat effect id %d\n", effectID ); return SCRIPT_CMD_FAILURE; } - ARR_FIND( 0, sd->hatEffectCount, i, sd->hatEffectIDs[i] == effectID ); + auto it = util::vector_get( sd->hatEffects, effectID ); if( enable ){ - if( i < sd->hatEffectCount ){ + if( it != sd->hatEffects.end() ){ return SCRIPT_CMD_SUCCESS; } - RECREATE(sd->hatEffectIDs,uint32,sd->hatEffectCount+1); - sd->hatEffectIDs[sd->hatEffectCount] = effectID; - sd->hatEffectCount++; + sd->hatEffects.push_back( effectID ); }else{ - if( i == sd->hatEffectCount ){ + if( it == sd->hatEffects.end() ){ return SCRIPT_CMD_SUCCESS; } - for( ; i < sd->hatEffectCount - 1; i++ ){ - sd->hatEffectIDs[i] = sd->hatEffectIDs[i+1]; - } - - sd->hatEffectCount--; - - if( !sd->hatEffectCount ){ - aFree(sd->hatEffectIDs); - sd->hatEffectIDs = NULL; - } + util::vector_erase_if_exists( sd->hatEffects, effectID ); } if( !sd->state.connect_new ){ diff --git a/src/map/script.hpp b/src/map/script.hpp index 83e3672b02..804d003329 100644 --- a/src/map/script.hpp +++ b/src/map/script.hpp @@ -1842,7 +1842,7 @@ enum e_special_effects { EF_MAX }; -enum e_hat_effects { +enum e_hat_effects : int16{ HAT_EF_MIN = 0, HAT_EF_BLOSSOM_FLUTTERING, HAT_EF_MERMAID_LONGING, diff --git a/src/map/unit.cpp b/src/map/unit.cpp index c67d6740bd..ca8b0b349e 100644 --- a/src/map/unit.cpp +++ b/src/map/unit.cpp @@ -3401,12 +3401,8 @@ int unit_free(struct block_list *bl, clr_type clrtype) } sd->qi_count = 0; -#if PACKETVER >= 20150513 - if( sd->hatEffectCount > 0 ){ - aFree(sd->hatEffectIDs); - sd->hatEffectIDs = NULL; - sd->hatEffectCount = 0; - } +#if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO) + sd->hatEffects.clear(); #endif if (sd->achievement_data.achievements)