diff options
Diffstat (limited to 'src/map/vending.c')
-rw-r--r-- | src/map/vending.c | 183 |
1 files changed, 86 insertions, 97 deletions
diff --git a/src/map/vending.c b/src/map/vending.c index 1f96672f4..1576b684e 100644 --- a/src/map/vending.c +++ b/src/map/vending.c @@ -1,5 +1,6 @@ -// Copyright (c) Athena Dev Teams - Licensed under GNU GPL -// For more information, see LICENCE in the main folder +// Copyright (c) Hercules Dev Team, licensed under GNU GPL. +// See the LICENSE file +// Portions Copyright (c) Athena Dev Teams #include "../common/nullpo.h" #include "../common/strlib.h" @@ -20,33 +21,28 @@ #include <stdio.h> #include <string.h> -static int vending_nextid = 0; - /// Returns an unique vending shop id. -static int vending_getuid(void) -{ - return vending_nextid++; +static inline unsigned int getid(void) { + return vending->next_id++; } /*========================================== * Close shop *------------------------------------------*/ -void vending_closevending(struct map_session_data* sd) -{ +void vending_closevending(struct map_session_data* sd) { nullpo_retv(sd); - if( sd->state.vending ) - { + if( sd->state.vending ) { sd->state.vending = false; clif->closevendingboard(&sd->bl, 0); + idb_remove(vending->db, sd->status.char_id); } } /*========================================== * Request a shop's item list *------------------------------------------*/ -void vending_vendinglistreq(struct map_session_data* sd, int id) -{ +void vending_vendinglistreq(struct map_session_data* sd, unsigned int id) { struct map_session_data* vsd; nullpo_retv(sd); @@ -55,8 +51,8 @@ void vending_vendinglistreq(struct map_session_data* sd, int id) if( !vsd->state.vending ) return; // not vending - if (!pc_can_give_items(sd) || !pc_can_give_items(vsd)) //check if both GMs are allowed to trade - { // GM is not allowed to trade + if (!pc_can_give_items(sd) || !pc_can_give_items(vsd)) { //check if both GMs are allowed to trade + // GM is not allowed to trade clif->message(sd->fd, msg_txt(246)); return; } @@ -69,27 +65,25 @@ void vending_vendinglistreq(struct map_session_data* sd, int id) /*========================================== * Purchase item(s) from a shop *------------------------------------------*/ -void vending_purchasereq(struct map_session_data* sd, int aid, int uid, const uint8* data, int count) -{ +void vending_purchasereq(struct map_session_data* sd, int aid, unsigned int uid, const uint8* data, int count) { int i, j, cursor, w, new_ = 0, blank, vend_list[MAX_VENDING]; double z; - struct s_vending vending[MAX_VENDING]; // against duplicate packets + struct s_vending vend[MAX_VENDING]; // against duplicate packets struct map_session_data* vsd = map_id2sd(aid); nullpo_retv(sd); if( vsd == NULL || !vsd->state.vending || vsd->bl.id == sd->bl.id ) return; // invalid shop - if( vsd->vender_id != uid ) - {// shop has changed + if( vsd->vender_id != uid ) { // shop has changed clif->buyvending(sd, 0, 0, 6); // store information was incorrect return; } - if( !searchstore_queryremote(sd, aid) && ( sd->bl.m != vsd->bl.m || !check_distance_bl(&sd->bl, &vsd->bl, AREA_SIZE) ) ) + if( !searchstore->queryremote(sd, aid) && ( sd->bl.m != vsd->bl.m || !check_distance_bl(&sd->bl, &vsd->bl, AREA_SIZE) ) ) return; // shop too far away - searchstore_clearremote(sd); + searchstore->clearremote(sd); if( count < 1 || count > MAX_VENDING || count > vsd->vend_num ) return; // invalid amount of purchased items @@ -97,13 +91,12 @@ void vending_purchasereq(struct map_session_data* sd, int aid, int uid, const ui blank = pc_inventoryblank(sd); //number of free cells in the buyer's inventory // duplicate item in vending to check hacker with multiple packets - memcpy(&vending, &vsd->vending, sizeof(vsd->vending)); // copy vending list + memcpy(&vend, &vsd->vending, sizeof(vsd->vending)); // copy vending list // some checks z = 0.; // zeny counter w = 0; // weight counter - for( i = 0; i < count; i++ ) - { + for( i = 0; i < count; i++ ) { short amount = *(uint16*)(data + 4*i + 0); short idx = *(uint16*)(data + 4*i + 2); idx -= 2; @@ -122,49 +115,45 @@ void vending_purchasereq(struct map_session_data* sd, int aid, int uid, const ui vend_list[i] = j; z += ((double)vsd->vending[j].value * (double)amount); - if( z > (double)sd->status.zeny || z < 0. || z > (double)MAX_ZENY ) - { + if( z > (double)sd->status.zeny || z < 0. || z > (double)MAX_ZENY ) { clif->buyvending(sd, idx, amount, 1); // you don't have enough zeny return; } - if( z + (double)vsd->status.zeny > (double)MAX_ZENY && !battle_config.vending_over_max ) - { + if( z + (double)vsd->status.zeny > (double)MAX_ZENY && !battle_config.vending_over_max ) { clif->buyvending(sd, idx, vsd->vending[j].amount, 4); // too much zeny = overflow return; } w += itemdb_weight(vsd->status.cart[idx].nameid) * amount; - if( w + sd->weight > sd->max_weight ) - { + if( w + sd->weight > sd->max_weight ) { clif->buyvending(sd, idx, amount, 2); // you can not buy, because overweight return; } //Check to see if cart/vend info is in sync. - if( vending[j].amount > vsd->status.cart[idx].amount ) - vending[j].amount = vsd->status.cart[idx].amount; + if( vend[j].amount > vsd->status.cart[idx].amount ) + vend[j].amount = vsd->status.cart[idx].amount; // if they try to add packets (example: get twice or more 2 apples if marchand has only 3 apples). // here, we check cumulative amounts - if( vending[j].amount < amount ) - { + if( vend[j].amount < amount ) { // send more quantity is not a hack (an other player can have buy items just before) clif->buyvending(sd, idx, vsd->vending[j].amount, 4); // not enough quantity return; } - vending[j].amount -= amount; + vend[j].amount -= amount; switch( pc_checkadditem(sd, vsd->status.cart[idx].nameid, amount) ) { - case ADDITEM_EXIST: - break; //We'd add this item to the existing one (in buyers inventory) - case ADDITEM_NEW: - new_++; - if (new_ > blank) - return; //Buyer has no space in his inventory - break; - case ADDITEM_OVERAMOUNT: - return; //too many items + case ADDITEM_EXIST: + break; //We'd add this item to the existing one (in buyers inventory) + case ADDITEM_NEW: + new_++; + if (new_ > blank) + return; //Buyer has no space in his inventory + break; + case ADDITEM_OVERAMOUNT: + return; //too many items } } @@ -173,8 +162,7 @@ void vending_purchasereq(struct map_session_data* sd, int aid, int uid, const ui z -= z * (battle_config.vending_tax/10000.); pc_getzeny(vsd, (int)z, LOG_TYPE_VENDING, sd); - for( i = 0; i < count; i++ ) - { + for( i = 0; i < count; i++ ) { short amount = *(uint16*)(data + 4*i + 0); short idx = *(uint16*)(data + 4*i + 2); idx -= 2; @@ -186,8 +174,7 @@ void vending_purchasereq(struct map_session_data* sd, int aid, int uid, const ui clif->vendingreport(vsd, idx, amount); //print buyer's name - if( battle_config.buyer_name ) - { + if( battle_config.buyer_name ) { char temp[256]; sprintf(temp, msg_txt(265), sd->status.name); clif->disp_onlyself(vsd,temp,strlen(temp)); @@ -195,13 +182,11 @@ void vending_purchasereq(struct map_session_data* sd, int aid, int uid, const ui } // compact the vending list - for( i = 0, cursor = 0; i < vsd->vend_num; i++ ) - { + for( i = 0, cursor = 0; i < vsd->vend_num; i++ ) { if( vsd->vending[i].amount == 0 ) continue; - if( cursor != i ) // speedup - { + if( cursor != i ) { // speedup vsd->vending[cursor].index = vsd->vending[i].index; vsd->vending[cursor].amount = vsd->vending[i].amount; vsd->vending[cursor].value = vsd->vending[i].value; @@ -212,21 +197,18 @@ void vending_purchasereq(struct map_session_data* sd, int aid, int uid, const ui vsd->vend_num = cursor; //Always save BOTH: buyer and customer - if( save_settings&2 ) - { + if( save_settings&2 ) { chrif_save(sd,0); chrif_save(vsd,0); } //check for @AUTOTRADE users [durf] - if( vsd->state.autotrade ) - { + if( vsd->state.autotrade ) { //see if there is anything left in the shop ARR_FIND( 0, vsd->vend_num, i, vsd->vending[i].amount > 0 ); - if( i == vsd->vend_num ) - { + if( i == vsd->vend_num ) { //Close Vending (this was automatically done by the client, we have to do it manually for autovenders) [Skotlex] - vending_closevending(vsd); + vending->close(vsd); map_quit(vsd); //They have no reason to stay around anymore, do they? } } @@ -246,23 +228,21 @@ void vending_openvending(struct map_session_data* sd, const char* message, const vending_skill_lvl = pc_checkskill(sd, MC_VENDING); // skill level and cart check - if( !vending_skill_lvl || !pc_iscarton(sd) ) - { + if( !vending_skill_lvl || !pc_iscarton(sd) ) { clif->skill_fail(sd, MC_VENDING, USESKILL_FAIL_LEVEL, 0); return; } // check number of items in shop - if( count < 1 || count > MAX_VENDING || count > 2 + vending_skill_lvl ) - { // invalid item count + if( count < 1 || count > MAX_VENDING || count > 2 + vending_skill_lvl ) { + // invalid item count clif->skill_fail(sd, MC_VENDING, USESKILL_FAIL_LEVEL, 0); return; } // filter out invalid items i = 0; - for( j = 0; j < count; j++ ) - { + for( j = 0; j < count; j++ ) { short index = *(uint16*)(data + 8*j + 0); short amount = *(uint16*)(data + 8*j + 2); unsigned int value = *(uint32*)(data + 8*j + 4); @@ -288,35 +268,33 @@ void vending_openvending(struct map_session_data* sd, const char* message, const if( i != j ) clif->message (sd->fd, msg_txt(266)); //"Some of your items cannot be vended and were removed from the shop." - if( i == 0 ) - { // no valid item found + if( i == 0 ) { // no valid item found clif->skill_fail(sd, MC_VENDING, USESKILL_FAIL_LEVEL, 0); // custom reply packet return; } sd->state.prevend = 0; sd->state.vending = true; - sd->vender_id = vending_getuid(); + sd->vender_id = getid(); sd->vend_num = i; safestrncpy(sd->message, message, MESSAGE_SIZE); clif->openvending(sd,sd->bl.id,sd->vending); clif->showvendingboard(&sd->bl,message,0); + + idb_put(vending->db, sd->vender_id, sd); } /// Checks if an item is being sold in given player's vending. -bool vending_search(struct map_session_data* sd, unsigned short nameid) -{ +bool vending_search(struct map_session_data* sd, unsigned short nameid) { int i; - if( !sd->state.vending ) - {// not vending + if( !sd->state.vending ) { // not vending return false; } ARR_FIND( 0, sd->vend_num, i, sd->status.cart[sd->vending[i].index].nameid == (short)nameid ); - if( i == sd->vend_num ) - {// not found + if( i == sd->vend_num ) { // not found return false; } @@ -326,46 +304,36 @@ bool vending_search(struct map_session_data* sd, unsigned short nameid) /// Searches for all items in a vending, that match given ids, price and possible cards. /// @return Whether or not the search should be continued. -bool vending_searchall(struct map_session_data* sd, const struct s_search_store_search* s) -{ +bool vending_searchall(struct map_session_data* sd, const struct s_search_store_search* s) { int i, c, slot; unsigned int idx, cidx; struct item* it; - if( !sd->state.vending ) - {// not vending + if( !sd->state.vending ) // not vending return true; - } - for( idx = 0; idx < s->item_count; idx++ ) - { + for( idx = 0; idx < s->item_count; idx++ ) { ARR_FIND( 0, sd->vend_num, i, sd->status.cart[sd->vending[i].index].nameid == (short)s->itemlist[idx] ); - if( i == sd->vend_num ) - {// not found + if( i == sd->vend_num ) {// not found continue; } it = &sd->status.cart[sd->vending[i].index]; - if( s->min_price && s->min_price > sd->vending[i].value ) - {// too low price + if( s->min_price && s->min_price > sd->vending[i].value ) {// too low price continue; } - if( s->max_price && s->max_price < sd->vending[i].value ) - {// too high price + if( s->max_price && s->max_price < sd->vending[i].value ) {// too high price continue; } - if( s->card_count ) - {// check cards - if( itemdb_isspecial(it->card[0]) ) - {// something, that is not a carded + if( s->card_count ) {// check cards + if( itemdb_isspecial(it->card[0]) ) {// something, that is not a carded continue; } slot = itemdb_slot(it->nameid); - for( c = 0; c < slot && it->card[c]; c ++ ) - { + for( c = 0; c < slot && it->card[c]; c ++ ) { ARR_FIND( 0, s->card_count, cidx, s->cardlist[cidx] == it->card[c] ); if( cidx != s->card_count ) {// found @@ -373,13 +341,12 @@ bool vending_searchall(struct map_session_data* sd, const struct s_search_store_ } } - if( c == slot || !it->card[c] ) - {// no card match + if( c == slot || !it->card[c] ) {// no card match continue; } } - if( !searchstore_result(s->search_sd, sd->vender_id, sd->status.account_id, sd->message, it->nameid, sd->vending[i].amount, sd->vending[i].value, it->card, it->refine) ) + if( !searchstore->result(s->search_sd, sd->vender_id, sd->status.account_id, sd->message, it->nameid, sd->vending[i].amount, sd->vending[i].value, it->card, it->refine) ) {// result set full return false; } @@ -387,3 +354,25 @@ bool vending_searchall(struct map_session_data* sd, const struct s_search_store_ return true; } +void final(void) { + db_destroy(vending->db); +} + +void init(void) { + vending->db = idb_alloc(DB_OPT_BASE); + vending->next_id = 0; +} + +void vending_defaults(void) { + vending = &vending_s; + + vending->init = init; + vending->final = final; + + vending->close = vending_closevending; + vending->open = vending_openvending; + vending->list = vending_vendinglistreq; + vending->purchase = vending_purchasereq; + vending->search = vending_search; + vending->searchall = vending_searchall; +} |