diff options
author | Happy <markaizer@gmail.com> | 2014-08-21 04:50:46 +0800 |
---|---|---|
committer | Happy <markaizer@gmail.com> | 2014-08-21 04:50:46 +0800 |
commit | f52e1007fe08c67003c0bc4c78231904dd3fd5cc (patch) | |
tree | 99907d827264e501774e58ab4630e41fa7103c02 /src/map/storage.c | |
parent | 2410110dece79b4598c12f1c953219f1d0d1904a (diff) | |
parent | 769b1d05aa5cfa8cddfe7d21b35d5c5e4da3bbd6 (diff) | |
download | hercules-f52e1007fe08c67003c0bc4c78231904dd3fd5cc.tar.gz hercules-f52e1007fe08c67003c0bc4c78231904dd3fd5cc.tar.bz2 hercules-f52e1007fe08c67003c0bc4c78231904dd3fd5cc.tar.xz hercules-f52e1007fe08c67003c0bc4c78231904dd3fd5cc.zip |
Merge pull request #1 from HerculesWS/master
Update from original
Diffstat (limited to 'src/map/storage.c')
-rw-r--r-- | src/map/storage.c | 276 |
1 files changed, 162 insertions, 114 deletions
diff --git a/src/map/storage.c b/src/map/storage.c index ea30f6c0f..217f14a3a 100644 --- a/src/map/storage.c +++ b/src/map/storage.c @@ -1,38 +1,40 @@ -// 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/cbasetypes.h" -#include "../common/db.h" -#include "../common/nullpo.h" -#include "../common/malloc.h" -#include "../common/showmsg.h" +#define HERCULES_CORE -#include "map.h" // struct map_session_data #include "storage.h" -#include "chrif.h" -#include "itemdb.h" -#include "clif.h" -#include "intif.h" -#include "pc.h" -#include "guild.h" -#include "battle.h" -#include "atcommand.h" -#include "log.h" #include <stdio.h> #include <stdlib.h> #include <string.h> +#include "atcommand.h" +#include "battle.h" +#include "chrif.h" +#include "clif.h" +#include "guild.h" +#include "intif.h" +#include "itemdb.h" +#include "log.h" +#include "map.h" // struct map_session_data +#include "pc.h" +#include "../common/cbasetypes.h" +#include "../common/db.h" +#include "../common/malloc.h" +#include "../common/nullpo.h" -static DBMap* guild_storage_db; // int guild_id -> struct guild_storage* +struct storage_interface storage_s; +struct guild_storage_interface gstorage_s; /*========================================== * Sort items in the warehouse *------------------------------------------*/ -static int storage_comp_item(const void *_i1, const void *_i2) +int storage_comp_item(const void *i1_, const void *i2_) { - struct item *i1 = (struct item *)_i1; - struct item *i2 = (struct item *)_i2; + struct item *i1 = (struct item *)i1_; + struct item *i2 = (struct item *)i2_; if (i1->nameid == i2->nameid) return 0; @@ -44,46 +46,32 @@ static int storage_comp_item(const void *_i1, const void *_i2) } //Sort item by storage_comp_item (nameid) -static void storage_sortitem(struct item* items, unsigned int size) +void storage_sortitem(struct item* items, unsigned int size) { nullpo_retv(items); if( battle_config.client_sort_storage ) { - qsort(items, size, sizeof(struct item), storage_comp_item); + qsort(items, size, sizeof(struct item), storage->comp_item); } } -/*========================================== - * Init/Terminate - *------------------------------------------*/ -int do_init_storage(void) // Called from map.c::do_init() -{ - guild_storage_db=idb_alloc(DB_OPT_RELEASE_DATA); - return 1; -} -void do_final_storage(void) // by [MC Cameri] -{ - guild_storage_db->destroy(guild_storage_db,NULL); -} - /** * Parses storage and saves 'dirty' ones upon reconnect. [Skotlex] * @see DBApply */ -static int storage_reconnect_sub(DBKey key, DBData *data, va_list ap) +int storage_reconnect_sub(DBKey key, DBData *data, va_list ap) { struct guild_storage *stor = DB->data2ptr(data); if (stor->dirty && stor->storage_status == 0) //Save closed storages. - storage_guild_storagesave(0, stor->guild_id,0); + gstorage->save(0, stor->guild_id,0); return 0; } //Function to be invoked upon server reconnection to char. To save all 'dirty' storages [Skotlex] -void do_reconnect_storage(void) -{ - guild_storage_db->foreach(guild_storage_db, storage_reconnect_sub); +void do_reconnect_storage(void) { + gstorage->db->foreach(gstorage->db, storage->reconnect_sub); } /*========================================== @@ -98,14 +86,14 @@ int storage_storageopen(struct map_session_data *sd) if(sd->state.storage_flag) return 1; //Already open? - if( !pc->can_give_items(sd) ) + if( !pc_can_give_items(sd) ) { //check is this GM level is allowed to put items to storage clif->message(sd->fd, msg_txt(246)); return 1; } sd->state.storage_flag = 1; - storage_sortitem(sd->status.storage.items, ARRAYLENGTH(sd->status.storage.items)); + storage->sortitem(sd->status.storage.items, ARRAYLENGTH(sd->status.storage.items)); clif->storagelist(sd, sd->status.storage.items, ARRAYLENGTH(sd->status.storage.items)); clif->updatestorageamount(sd, sd->status.storage.storage_amount, MAX_STORAGE); return 0; @@ -120,7 +108,8 @@ int compare_item(struct item *a, struct item *b) a->identify == b->identify && a->refine == b->refine && a->attribute == b->attribute && - a->expire_time == b->expire_time ) + a->expire_time == b->expire_time && + a->bound == b->bound ) { int i; for (i = 0; i < MAX_SLOTS && (a->card[i] == b->card[i]); i++); @@ -132,8 +121,7 @@ int compare_item(struct item *a, struct item *b) /*========================================== * Internal add-item function. *------------------------------------------*/ -static int storage_additem(struct map_session_data* sd, struct item* item_data, int amount) -{ +int storage_additem(struct map_session_data* sd, struct item* item_data, int amount) { struct storage_data* stor = &sd->status.storage; struct item_data *data; int i; @@ -141,20 +129,25 @@ static int storage_additem(struct map_session_data* sd, struct item* item_data, if( item_data->nameid <= 0 || amount <= 0 ) return 1; - data = itemdb_search(item_data->nameid); + data = itemdb->search(item_data->nameid); if( data->stack.storage && amount > data->stack.amount ) {// item stack limitation return 1; } - if( !itemdb_canstore(item_data, pc->get_group_level(sd)) ) + if( !itemdb_canstore(item_data, pc_get_group_level(sd)) ) { //Check if item is storable. [Skotlex] clif->message (sd->fd, msg_txt(264)); return 1; } - if( itemdb_isstackable2(data) ) + if( item_data->bound > IBT_ACCOUNT && !pc_can_give_bound_items(sd) ) { + clif->message(sd->fd, msg_txt(294)); + return 1; + } + + if( itemdb->isstackable2(data) ) {//Stackable for( i = 0; i < MAX_STORAGE; i++ ) { @@ -210,8 +203,7 @@ int storage_delitem(struct map_session_data* sd, int n, int amount) * 0 : fail * 1 : success *------------------------------------------*/ -int storage_storageadd(struct map_session_data* sd, int index, int amount) -{ +int storage_storageadd(struct map_session_data* sd, int index, int amount) { nullpo_ret(sd); if( sd->status.storage.storage_amount > MAX_STORAGE ) @@ -226,8 +218,10 @@ int storage_storageadd(struct map_session_data* sd, int index, int amount) if( amount < 1 || amount > sd->status.inventory[index].amount ) return 0; - if( storage_additem(sd,&sd->status.inventory[index],amount) == 0 ) + if( storage->additem(sd,&sd->status.inventory[index],amount) == 0 ) pc->delitem(sd,index,amount,0,4,LOG_TYPE_STORAGE); + else + clif->dropitem(sd, index,0); return 1; } @@ -253,7 +247,7 @@ int storage_storageget(struct map_session_data* sd, int index, int amount) return 0; if( (flag = pc->additem(sd,&sd->status.storage.items[index],amount,LOG_TYPE_STORAGE)) == 0 ) - storage_delitem(sd,index,amount); + storage->delitem(sd,index,amount); else clif->additem(sd,0,0,flag); @@ -283,7 +277,7 @@ int storage_storageaddfromcart(struct map_session_data* sd, int index, int amoun if( amount < 1 || amount > sd->status.cart[index].amount ) return 0; - if( storage_additem(sd,&sd->status.cart[index],amount) == 0 ) + if( storage->additem(sd,&sd->status.cart[index],amount) == 0 ) pc->cart_delitem(sd,index,amount,0,LOG_TYPE_STORAGE); return 1; @@ -296,8 +290,8 @@ int storage_storageaddfromcart(struct map_session_data* sd, int index, int amoun * 0 : fail * 1 : success *------------------------------------------*/ -int storage_storagegettocart(struct map_session_data* sd, int index, int amount) -{ +int storage_storagegettocart(struct map_session_data* sd, int index, int amount) { + int flag = 0; nullpo_ret(sd); if( index < 0 || index >= MAX_STORAGE ) @@ -309,8 +303,12 @@ int storage_storagegettocart(struct map_session_data* sd, int index, int amount) if( amount < 1 || amount > sd->status.storage.items[index].amount ) return 0; - if( pc->cart_additem(sd,&sd->status.storage.items[index],amount,LOG_TYPE_STORAGE) == 0 ) - storage_delitem(sd,index,amount); + if( (flag = pc->cart_additem(sd,&sd->status.storage.items[index],amount,LOG_TYPE_STORAGE)) == 0 ) + storage->delitem(sd,index,amount); + else { + clif->dropitem(sd, index,0); + clif->cart_additem_ack(sd,flag == 1?0x0:0x1); + } return 1; } @@ -319,14 +317,13 @@ int storage_storagegettocart(struct map_session_data* sd, int index, int amount) /*========================================== * Modified By Valaris to save upon closing [massdriller] *------------------------------------------*/ -void storage_storageclose(struct map_session_data* sd) -{ +void storage_storageclose(struct map_session_data* sd) { nullpo_retv(sd); clif->storageclose(sd); - if( iMap->save_settings&4 ) - chrif_save(sd,0); //Invokes the storage saving as well. + if( map->save_settings&4 ) + chrif->save(sd,0); //Invokes the storage saving as well. sd->state.storage_flag = 0; } @@ -334,12 +331,11 @@ void storage_storageclose(struct map_session_data* sd) /*========================================== * When quitting the game. *------------------------------------------*/ -void storage_storage_quit(struct map_session_data* sd, int flag) -{ +void storage_storage_quit(struct map_session_data* sd, int flag) { nullpo_retv(sd); - if (iMap->save_settings&4) - chrif_save(sd, flag); //Invokes the storage saving as well. + if (map->save_settings&4) + chrif->save(sd, flag); //Invokes the storage saving as well. sd->state.storage_flag = 0; } @@ -347,7 +343,7 @@ void storage_storage_quit(struct map_session_data* sd, int flag) /** * @see DBCreateData */ -static DBData create_guildstorage(DBKey key, va_list args) +DBData create_guildstorage(DBKey key, va_list args) { struct guild_storage *gs = NULL; gs = (struct guild_storage *) aCalloc(sizeof(struct guild_storage), 1); @@ -359,19 +355,17 @@ struct guild_storage *guild2storage(int guild_id) { struct guild_storage *gs = NULL; if(guild->search(guild_id) != NULL) - gs = idb_ensure(guild_storage_db,guild_id,create_guildstorage); + gs = idb_ensure(gstorage->db,guild_id,gstorage->create); return gs; } //For just locating a storage without creating one. [Skotlex] -struct guild_storage *guild2storage2(int guild_id) -{ - return (struct guild_storage*)idb_get(guild_storage_db,guild_id); +struct guild_storage *guild2storage2(int guild_id) { + return (struct guild_storage*)idb_get(gstorage->db,guild_id); } -int guild_storage_delete(int guild_id) -{ - idb_remove(guild_storage_db,guild_id); +int guild_storage_delete(int guild_id) { + idb_remove(gstorage->db,guild_id); return 0; } @@ -394,13 +388,13 @@ int storage_guild_storageopen(struct map_session_data* sd) if(sd->state.storage_flag) return 1; //Can't open both storages at a time. - if( !pc->can_give_items(sd) ) { //check is this GM level can open guild storage and store items [Lupus] + if( !pc_can_give_items(sd) ) { //check is this GM level can open guild storage and store items [Lupus] clif->message(sd->fd, msg_txt(246)); return 1; } - if((gstor = guild2storage2(sd->status.guild_id)) == NULL) { - intif_request_guild_storage(sd->status.account_id,sd->status.guild_id); + if((gstor = gstorage->id2storage2(sd->status.guild_id)) == NULL) { + intif->request_guild_storage(sd->status.account_id,sd->status.guild_id); return 0; } if(gstor->storage_status) @@ -411,7 +405,7 @@ int storage_guild_storageopen(struct map_session_data* sd) gstor->storage_status = 1; sd->state.storage_flag = 2; - storage_sortitem(gstor->items, ARRAYLENGTH(gstor->items)); + storage->sortitem(gstor->items, ARRAYLENGTH(gstor->items)); clif->storagelist(sd, gstor->items, ARRAYLENGTH(gstor->items)); clif->updatestorageamount(sd, gstor->storage_amount, MAX_GUILD_STORAGE); return 0; @@ -435,20 +429,25 @@ int guild_storage_additem(struct map_session_data* sd, struct guild_storage* sto if(item_data->nameid <= 0 || amount <= 0) return 1; - data = itemdb_search(item_data->nameid); + data = itemdb->search(item_data->nameid); if( data->stack.guildstorage && amount > data->stack.amount ) {// item stack limitation return 1; } - if( !itemdb_canguildstore(item_data, pc->get_group_level(sd)) || item_data->expire_time ) - { //Check if item is storable. [Skotlex] + if( !itemdb_canguildstore(item_data, pc_get_group_level(sd)) || item_data->expire_time ) + { //Check if item is storable. [Skotlex] clif->message (sd->fd, msg_txt(264)); return 1; } - if(itemdb_isstackable2(data)){ //Stackable + if( item_data->bound && item_data->bound != IBT_GUILD && !pc_can_give_bound_items(sd) ) { + clif->message(sd->fd, msg_txt(294)); + return 1; + } + + if(itemdb->isstackable2(data)){ //Stackable for(i=0;i<MAX_GUILD_STORAGE;i++){ if(compare_item(&stor->items[i], item_data)) { if( amount > MAX_AMOUNT - stor->items[i].amount || ( data->stack.guildstorage && amount > data->stack.amount - stor->items[i].amount ) ) @@ -512,7 +511,7 @@ int storage_guild_storageadd(struct map_session_data* sd, int index, int amount) struct guild_storage *stor; nullpo_ret(sd); - nullpo_ret(stor=guild2storage2(sd->status.guild_id)); + nullpo_ret(stor=gstorage->id2storage2(sd->status.guild_id)); if( !stor->storage_status || stor->storage_amount > MAX_GUILD_STORAGE ) return 0; @@ -527,12 +526,14 @@ int storage_guild_storageadd(struct map_session_data* sd, int index, int amount) return 0; if( stor->lock ) { - storage_guild_storageclose(sd); + gstorage->close(sd); return 0; } - if(guild_storage_additem(sd,stor,&sd->status.inventory[index],amount)==0) + if(gstorage->additem(sd,stor,&sd->status.inventory[index],amount)==0) pc->delitem(sd,index,amount,0,4,LOG_TYPE_GSTORAGE); + else + clif->dropitem(sd, index,0); return 1; } @@ -542,7 +543,7 @@ int storage_guild_storageadd(struct map_session_data* sd, int index, int amount) * @index : storage idx * return * 0 : fail -* 1 : succes +* 1 : success *------------------------------------------*/ int storage_guild_storageget(struct map_session_data* sd, int index, int amount) { @@ -565,12 +566,12 @@ int storage_guild_storageget(struct map_session_data* sd, int index, int amount) return 0; if( stor->lock ) { - storage_guild_storageclose(sd); + gstorage->close(sd); return 0; } if((flag = pc->additem(sd,&stor->items[index],amount,LOG_TYPE_GSTORAGE)) == 0) - guild_storage_delitem(sd,stor,index,amount); + gstorage->delitem(sd,stor,index,amount); else //inform fail clif->additem(sd,0,0,flag); // log_fromstorage(sd, index, 1); @@ -583,7 +584,7 @@ int storage_guild_storageget(struct map_session_data* sd, int index, int amount) * @index : cart inventory idx * return * 0 : fail -* 1 : succes +* 1 : success *------------------------------------------*/ int storage_guild_storageaddfromcart(struct map_session_data* sd, int index, int amount) { @@ -604,7 +605,7 @@ int storage_guild_storageaddfromcart(struct map_session_data* sd, int index, int if( amount < 1 || amount > sd->status.cart[index].amount ) return 0; - if(guild_storage_additem(sd,stor,&sd->status.cart[index],amount)==0) + if(gstorage->additem(sd,stor,&sd->status.cart[index],amount)==0) pc->cart_delitem(sd,index,amount,0,LOG_TYPE_GSTORAGE); return 1; @@ -615,7 +616,7 @@ int storage_guild_storageaddfromcart(struct map_session_data* sd, int index, int * @index : storage idx * return * 0 : fail -* 1 : succes +* 1 : success *------------------------------------------*/ int storage_guild_storagegettocart(struct map_session_data* sd, int index, int amount) { @@ -637,7 +638,7 @@ int storage_guild_storagegettocart(struct map_session_data* sd, int index, int a return 0; if(pc->cart_additem(sd,&stor->items[index],amount,LOG_TYPE_GSTORAGE)==0) - guild_storage_delitem(sd,stor,index,amount); + gstorage->delitem(sd,stor,index,amount); return 1; } @@ -646,7 +647,7 @@ int storage_guild_storagegettocart(struct map_session_data* sd, int index, int a * Request to save guild storage * return * 0 : fail (no storage) -* 1 : succes +* 1 : success *------------------------------------------*/ int storage_guild_storagesave(int account_id, int guild_id, int flag) { @@ -657,7 +658,7 @@ int storage_guild_storagesave(int account_id, int guild_id, int flag) if (flag) //Char quitting, close it. stor->storage_status = 0; if (stor->dirty) - intif_send_guild_storage(account_id,stor); + intif->send_guild_storage(account_id,stor); return 1; } return 0; @@ -667,13 +668,13 @@ int storage_guild_storagesave(int account_id, int guild_id, int flag) * ACK save of guild storage * return * 0 : fail (no storage) -* 1 : succes +* 1 : success *------------------------------------------*/ int storage_guild_storagesaved(int guild_id) { struct guild_storage *stor; - if((stor=guild2storage2(guild_id)) != NULL) { + if((stor=gstorage->id2storage2(guild_id)) != NULL) { if (stor->dirty && stor->storage_status == 0) { //Storage has been correctly saved. stor->dirty = 0; @@ -684,20 +685,18 @@ int storage_guild_storagesaved(int guild_id) } //Close storage for sd and save it -int storage_guild_storageclose(struct map_session_data* sd) -{ +int storage_guild_storageclose(struct map_session_data* sd) { struct guild_storage *stor; nullpo_ret(sd); - nullpo_ret(stor=guild2storage2(sd->status.guild_id)); + nullpo_ret(stor=gstorage->id2storage2(sd->status.guild_id)); clif->storageclose(sd); - if (stor->storage_status) - { - if (iMap->save_settings&4) - chrif_save(sd, 0); //This one also saves the storage. [Skotlex] + if (stor->storage_status) { + if (map->save_settings&4) + chrif->save(sd, 0); //This one also saves the storage. [Skotlex] else - storage_guild_storagesave(sd->status.account_id, sd->status.guild_id,0); + gstorage->save(sd->status.account_id, sd->status.guild_id,0); stor->storage_status=0; } sd->state.storage_flag = 0; @@ -705,31 +704,80 @@ int storage_guild_storageclose(struct map_session_data* sd) return 0; } -int storage_guild_storage_quit(struct map_session_data* sd, int flag) -{ +int storage_guild_storage_quit(struct map_session_data* sd, int flag) { struct guild_storage *stor; nullpo_ret(sd); - nullpo_ret(stor=guild2storage2(sd->status.guild_id)); + nullpo_ret(stor=gstorage->id2storage2(sd->status.guild_id)); - if(flag) - { //Only during a guild break flag is 1 (don't save storage) + if(flag) { + //Only during a guild break flag is 1 (don't save storage) sd->state.storage_flag = 0; stor->storage_status = 0; clif->storageclose(sd); - if (iMap->save_settings&4) - chrif_save(sd,0); + if (map->save_settings&4) + chrif->save(sd,0); return 0; } if(stor->storage_status) { - if (iMap->save_settings&4) - chrif_save(sd,0); + if (map->save_settings&4) + chrif->save(sd,0); else - storage_guild_storagesave(sd->status.account_id,sd->status.guild_id,1); + gstorage->save(sd->status.account_id,sd->status.guild_id,1); } sd->state.storage_flag = 0; stor->storage_status = 0; return 0; } +void do_init_gstorage(bool minimal) { + if (minimal) + return; + gstorage->db = idb_alloc(DB_OPT_RELEASE_DATA); +} +void do_final_gstorage(void) { + db_destroy(gstorage->db); +} +void storage_defaults(void) { + storage = &storage_s; + + /* */ + storage->reconnect = do_reconnect_storage; + /* */ + storage->delitem = storage_delitem; + storage->open = storage_storageopen; + storage->add = storage_storageadd; + storage->get = storage_storageget; + storage->additem = storage_additem; + storage->addfromcart = storage_storageaddfromcart; + storage->gettocart = storage_storagegettocart; + storage->close = storage_storageclose; + storage->pc_quit = storage_storage_quit; + storage->comp_item = storage_comp_item; + storage->sortitem = storage_sortitem; + storage->reconnect_sub = storage_reconnect_sub; +} +void gstorage_defaults(void) { + gstorage = &gstorage_s; + + /* */ + gstorage->init = do_init_gstorage; + gstorage->final = do_final_gstorage; + /* */ + gstorage->id2storage = guild2storage; + gstorage->id2storage2 = guild2storage2; + gstorage->delete = guild_storage_delete; + gstorage->open = storage_guild_storageopen; + gstorage->additem = guild_storage_additem; + gstorage->delitem = guild_storage_delitem; + gstorage->add = storage_guild_storageadd; + gstorage->get = storage_guild_storageget; + gstorage->addfromcart = storage_guild_storageaddfromcart; + gstorage->gettocart = storage_guild_storagegettocart; + gstorage->close = storage_guild_storageclose; + gstorage->pc_quit = storage_guild_storage_quit; + gstorage->save = storage_guild_storagesave; + gstorage->saved = storage_guild_storagesaved; + gstorage->create = create_guildstorage; +} |