123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761 |
- // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
- // For more information, see LICENCE in the main folder
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "../common/nullpo.h"
- #include "../common/malloc.h"
- #include "../common/showmsg.h"
- #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"
- static struct dbt *storage_db;
- static struct dbt *guild_storage_db;
- /*==========================================
- * 倉庫内アイテムソート
- *------------------------------------------
- */
- int storage_comp_item(const void *_i1, const void *_i2)
- {
- struct item *i1 = (struct item *)_i1;
- struct item *i2 = (struct item *)_i2;
- if (i1->nameid == i2->nameid)
- return 0;
- else if (!(i1->nameid) || !(i1->amount))
- return 1;
- else if (!(i2->nameid) || !(i2->amount))
- return -1;
- return i1->nameid - i2->nameid;
- }
-
- void sortage_sortitem (struct storage *stor)
- {
- nullpo_retv(stor);
- qsort(stor->storage_, MAX_STORAGE, sizeof(struct item), storage_comp_item);
- }
- void sortage_gsortitem (struct guild_storage* gstor)
- {
- nullpo_retv(gstor);
- qsort(gstor->storage_, MAX_GUILD_STORAGE, sizeof(struct item), storage_comp_item);
- }
- /*==========================================
- * 初期化とか
- *------------------------------------------
- */
- int do_init_storage(void) // map.c::do_init()から呼ばれる
- {
- storage_db=db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int));
- guild_storage_db=db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int));
- return 1;
- }
- void do_final_storage(void) // by [MC Cameri]
- {
- storage_db->destroy(storage_db,NULL);
- guild_storage_db->destroy(guild_storage_db,NULL);
- }
- static int storage_reconnect_sub(DBKey key,void *data,va_list ap)
- { //Parses storage and saves 'dirty' ones upon reconnect. [Skotlex]
- int type = va_arg(ap, int);
- if (type)
- { //Guild Storage
- struct guild_storage* stor = (struct guild_storage*) data;
- if (stor->dirty && stor->storage_status == 0) //Save closed storages.
- storage_guild_storagesave(0, stor->guild_id,0);
- }
- else
- { //Account Storage
- struct storage* stor = (struct storage*) data;
- if (stor->dirty && stor->storage_status == 0) //Save closed storages.
- storage_storage_save(stor->account_id, stor->dirty==2?1:0);
- }
- return 0;
- }
- //Function to be invoked upon server reconnection to char. To save all 'dirty' storages [Skotlex
- void do_reconnect_storage(void)
- {
- storage_db->foreach(storage_db, storage_reconnect_sub, 0);
- guild_storage_db->foreach(guild_storage_db, storage_reconnect_sub, 1);
- }
- static void* create_storage(DBKey key, va_list args) {
- struct storage *stor;
- stor = (struct storage *) aCallocA (sizeof(struct storage), 1);
- stor->account_id = key.i;
- return stor;
- }
- struct storage *account2storage(int account_id)
- {
- return idb_ensure(storage_db,account_id,create_storage);
- }
- // Just to ask storage, without creation
- struct storage *account2storage2(int account_id)
- {
- return idb_get(storage_db, account_id);
- }
- int storage_delete(int account_id)
- {
- idb_remove(storage_db,account_id);
- return 0;
- }
- /*==========================================
- * Opens a storage. Returns:
- * 0 - success
- * 1 - fail
- * 2 - Storage requested from char-server (will open automatically later)
- *------------------------------------------
- */
- int storage_storageopen(struct map_session_data *sd)
- {
- struct storage *stor;
- nullpo_retr(0, sd);
- if(sd->state.finalsave) //Refuse to open storage when you had your last save done.
- return 1;
- if(sd->state.storage_flag)
- return 1; //Already open?
-
- if(pc_can_give_items(pc_isGM(sd)))
- { //check is this GM level is allowed to put items to storage
- clif_displaymessage(sd->fd, msg_txt(246));
- return 1;
- }
-
- if((stor = idb_get(storage_db,sd->status.account_id)) == NULL)
- { //Request storage.
- intif_request_storage(sd->status.account_id);
- return 2;
- }
-
- if (stor->storage_status)
- return 1; //Already open/player already has it open...
- stor->storage_status = 1;
- sd->state.storage_flag = 1;
- clif_storagelist(sd,stor);
- clif_updatestorageamount(sd,stor);
- return 0;
- }
- /*==========================================
- * Internal add-item function.
- *------------------------------------------
- */
- static int storage_additem(struct map_session_data *sd,struct storage *stor,struct item *item_data,int amount)
- {
- struct item_data *data;
- int i;
- if (sd->state.finalsave)
- return 1;
- if(item_data->nameid <= 0 || amount <= 0)
- return 1;
-
- data = itemdb_search(item_data->nameid);
- if (!itemdb_canstore(item_data, pc_isGM(sd)))
- { //Check if item is storable. [Skotlex]
- clif_displaymessage (sd->fd, msg_txt(264));
- return 1;
- }
-
- if(itemdb_isstackable2(data)){ //Stackable
- for(i=0;i<MAX_STORAGE;i++){
- if( compare_item (&stor->storage_[i], item_data)) {
- if(amount > MAX_AMOUNT - stor->storage_[i].amount)
- return 1;
- stor->storage_[i].amount+=amount;
- clif_storageitemadded(sd,stor,i,amount);
- stor->dirty = 1;
- return 0;
- }
- }
- }
- //Add item
- for(i=0;i<MAX_STORAGE && stor->storage_[i].nameid;i++);
-
- if(i>=MAX_STORAGE)
- return 1;
- memcpy(&stor->storage_[i],item_data,sizeof(stor->storage_[0]));
- stor->storage_[i].amount=amount;
- stor->storage_amount++;
- clif_storageitemadded(sd,stor,i,amount);
- clif_updatestorageamount(sd,stor);
- stor->dirty = 1;
- return 0;
- }
- /*==========================================
- * Internal del-item function
- *------------------------------------------
- */
- static int storage_delitem(struct map_session_data *sd,struct storage *stor,int n,int amount)
- {
- if(stor->storage_[n].nameid==0 || stor->storage_[n].amount<amount)
- return 1;
- stor->storage_[n].amount-=amount;
- if(stor->storage_[n].amount==0){
- memset(&stor->storage_[n],0,sizeof(stor->storage_[0]));
- stor->storage_amount--;
- clif_updatestorageamount(sd,stor);
- }
- clif_storageitemremoved(sd,n,amount);
- stor->dirty = 1;
- return 0;
- }
- /*==========================================
- * Add an item to the storage from the inventory.
- *------------------------------------------
- */
- int storage_storageadd(struct map_session_data *sd,int index,int amount)
- {
- struct storage *stor;
- nullpo_retr(0, sd);
- nullpo_retr(0, stor=account2storage2(sd->status.account_id));
- if((stor->storage_amount > MAX_STORAGE) || !stor->storage_status)
- return 0; // storage full / storage closed
- if(index<0 || index>=MAX_INVENTORY)
- return 0;
- if(sd->status.inventory[index].nameid <= 0)
- return 0; //No item on that spot
-
- if(amount < 1 || amount > sd->status.inventory[index].amount)
- return 0;
- // log_tostorage(sd, index, 0);
- if(storage_additem(sd,stor,&sd->status.inventory[index],amount)==0)
- // remove item from inventory
- pc_delitem(sd,index,amount,0);
- return 1;
- }
- /*==========================================
- * Retrieve an item from the storage.
- *------------------------------------------
- */
- int storage_storageget(struct map_session_data *sd,int index,int amount)
- {
- struct storage *stor;
- int flag;
- nullpo_retr(0, sd);
- nullpo_retr(0, stor=account2storage2(sd->status.account_id));
-
- if(index<0 || index>=MAX_STORAGE)
- return 0;
- if(stor->storage_[index].nameid <= 0)
- return 0; //Nothing there
-
- if(amount < 1 || amount > stor->storage_[index].amount)
- return 0;
- if((flag = pc_additem(sd,&stor->storage_[index],amount)) == 0)
- storage_delitem(sd,stor,index,amount);
- else
- clif_additem(sd,0,0,flag);
- // log_fromstorage(sd, index, 0);
- return 1;
- }
- /*==========================================
- * Move an item from cart to storage.
- *------------------------------------------
- */
- int storage_storageaddfromcart(struct map_session_data *sd,int index,int amount)
- {
- struct storage *stor;
- nullpo_retr(0, sd);
- nullpo_retr(0, stor=account2storage2(sd->status.account_id));
- if(stor->storage_amount > MAX_STORAGE || !stor->storage_status)
- return 0; // storage full / storage closed
- if(index< 0 || index>=MAX_CART)
- return 0;
- if(sd->status.cart[index].nameid <= 0)
- return 0; //No item there.
-
- if(amount < 1 || amount > sd->status.cart[index].amount)
- return 0;
- if(storage_additem(sd,stor,&sd->status.cart[index],amount)==0)
- pc_cart_delitem(sd,index,amount,0);
- return 1;
- }
- /*==========================================
- * Get from Storage to the Cart
- *------------------------------------------
- */
- int storage_storagegettocart(struct map_session_data *sd,int index,int amount)
- {
- struct storage *stor;
- nullpo_retr(0, sd);
- nullpo_retr(0, stor=account2storage2(sd->status.account_id));
- if(!stor->storage_status)
- return 0;
-
- if(index< 0 || index>=MAX_STORAGE)
- return 0;
-
- if(stor->storage_[index].nameid <= 0)
- return 0; //Nothing there.
-
- if(amount < 1 || amount > stor->storage_[index].amount)
- return 0;
-
- if(pc_cart_additem(sd,&stor->storage_[index],amount)==0)
- storage_delitem(sd,stor,index,amount);
- return 1;
- }
- /*==========================================
- * Modified By Valaris to save upon closing [massdriller]
- *------------------------------------------
- */
- int storage_storageclose(struct map_session_data *sd)
- {
- struct storage *stor;
- nullpo_retr(0, sd);
- nullpo_retr(0, stor=account2storage2(sd->status.account_id));
- clif_storageclose(sd);
- if (stor->storage_status)
- {
- if (save_settings&4)
- chrif_save(sd,0); //Invokes the storage saving as well.
- else
- storage_storage_save(sd->status.account_id, 0);
- }
- stor->storage_status=0;
- sd->state.storage_flag=0;
- return 0;
- }
- /*==========================================
- * When quitting the game.
- *------------------------------------------
- */
- int storage_storage_quit(struct map_session_data *sd, int flag)
- {
- struct storage *stor;
- nullpo_retr(0, sd);
- nullpo_retr(0, stor=account2storage2(sd->status.account_id));
-
- if (stor->storage_status)
- {
- if (save_settings&4)
- chrif_save(sd, flag); //Invokes the storage saving as well.
- else
- storage_storage_save(sd->status.account_id, flag);
- }
- stor->storage_status = 0;
- sd->state.storage_flag = 0;
- return 0;
- }
- void storage_storage_dirty(struct map_session_data *sd)
- {
- struct storage *stor;
- stor=account2storage2(sd->status.account_id);
- if(stor)
- stor->dirty = 1;
- }
- int storage_storage_save(int account_id, int final)
- {
- struct storage *stor;
- stor=account2storage2(account_id);
- if(!stor) return 0;
- if(stor->dirty)
- {
- if (final) {
- stor->dirty = 2;
- stor->storage_status = 0; //To prevent further manipulation of it.
- }
- intif_send_storage(stor);
- return 1;
- }
- if (final)
- { //Clear storage from memory. Nothing to save.
- storage_delete(account_id);
- return 1;
- }
- return 0;
- }
- //Ack from Char-server indicating the storage was saved. [Skotlex]
- int storage_storage_saved(int account_id)
- {
- struct storage *stor;
-
- if((stor=account2storage2(account_id)) == NULL)
- return 0;
- if (stor->dirty == 2)
- { //Final save of storage. Remove from memory.
- storage_delete(account_id);
- return 1;
- }
- if (stor->dirty && stor->storage_status == 0)
- { //Only mark it clean if it's not in use. [Skotlex]
- stor->dirty = 0;
- sortage_sortitem(stor);
- return 1;
- }
- return 0;
- }
- static void* create_guildstorage(DBKey key, va_list args) {
- struct guild_storage *gs = NULL;
- gs = (struct guild_storage *) aCallocA(sizeof(struct guild_storage), 1);
- gs->guild_id=key.i;
- return gs;
- }
- struct guild_storage *guild2storage(int guild_id)
- {
- struct guild_storage *gs = NULL;
- if(guild_search(guild_id) != NULL)
- gs=(struct guild_storage *) idb_ensure(guild_storage_db,guild_id,create_guildstorage);
- return gs;
- }
- struct guild_storage *guild2storage2(int guild_id)
- { //For just locating a storage without creating one. [Skotlex]
- return idb_get(guild_storage_db,guild_id);
- }
- int guild_storage_delete(int guild_id)
- {
- idb_remove(guild_storage_db,guild_id);
- return 0;
- }
- int storage_guild_storageopen(struct map_session_data *sd)
- {
- struct guild_storage *gstor;
- nullpo_retr(0, sd);
- if(sd->status.guild_id <= 0)
- return 2;
- if(sd->state.finalsave) //Refuse to open storage when you had your last save done.
- return 1;
-
- if(sd->state.storage_flag)
- return 1; //Can't open both storages at a time.
-
- if( pc_can_give_items(pc_isGM(sd)) ) { //check is this GM level can open guild storage and store items [Lupus]
- clif_displaymessage(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);
- return 0;
- }
- if(gstor->storage_status)
- return 1;
-
- gstor->storage_status = 1;
- sd->state.storage_flag = 2;
- clif_guildstoragelist(sd,gstor);
- clif_updateguildstorageamount(sd,gstor);
- return 0;
- }
- int guild_storage_additem(struct map_session_data *sd,struct guild_storage *stor,struct item *item_data,int amount)
- {
- struct item_data *data;
- int i;
- nullpo_retr(1, sd);
- nullpo_retr(1, stor);
- nullpo_retr(1, item_data);
- nullpo_retr(1, data = itemdb_search(item_data->nameid));
- if(item_data->nameid <= 0 || amount <= 0)
- return 1;
- if (!itemdb_canguildstore(item_data, pc_isGM(sd)))
- { //Check if item is storable. [Skotlex]
- clif_displaymessage (sd->fd, msg_txt(264));
- return 1;
- }
- if(itemdb_isstackable2(data)){ //Stackable
- for(i=0;i<MAX_GUILD_STORAGE;i++){
- if(compare_item(&stor->storage_[i], item_data)) {
- if(stor->storage_[i].amount+amount > MAX_AMOUNT)
- return 1;
- stor->storage_[i].amount+=amount;
- clif_guildstorageitemadded(sd,stor,i,amount);
- stor->dirty = 1;
- return 0;
- }
- }
- }
- //Add item
- for(i=0;i<MAX_GUILD_STORAGE && stor->storage_[i].nameid;i++);
-
- if(i>=MAX_GUILD_STORAGE)
- return 1;
-
- memcpy(&stor->storage_[i],item_data,sizeof(stor->storage_[0]));
- stor->storage_[i].amount=amount;
- stor->storage_amount++;
- clif_guildstorageitemadded(sd,stor,i,amount);
- clif_updateguildstorageamount(sd,stor);
- stor->dirty = 1;
- return 0;
- }
- int guild_storage_delitem(struct map_session_data *sd,struct guild_storage *stor,int n,int amount)
- {
- nullpo_retr(1, sd);
- nullpo_retr(1, stor);
- if(stor->storage_[n].nameid==0 || stor->storage_[n].amount<amount)
- return 1;
- stor->storage_[n].amount-=amount;
- if(stor->storage_[n].amount==0){
- memset(&stor->storage_[n],0,sizeof(stor->storage_[0]));
- stor->storage_amount--;
- clif_updateguildstorageamount(sd,stor);
- }
- clif_storageitemremoved(sd,n,amount);
- stor->dirty = 1;
- return 0;
- }
- int storage_guild_storageadd(struct map_session_data *sd,int index,int amount)
- {
- struct guild_storage *stor;
- nullpo_retr(0, sd);
- nullpo_retr(0, stor=guild2storage2(sd->status.guild_id));
-
- if (!stor->storage_status || stor->storage_amount > MAX_GUILD_STORAGE)
- return 0;
-
- if(index<0 || index>=MAX_INVENTORY)
- return 0;
- if(sd->status.inventory[index].nameid <= 0)
- return 0;
-
- if(amount < 1 || amount > sd->status.inventory[index].amount)
- return 0;
- // log_tostorage(sd, index, 1);
- if(guild_storage_additem(sd,stor,&sd->status.inventory[index],amount)==0)
- pc_delitem(sd,index,amount,0);
- return 1;
- }
- int storage_guild_storageget(struct map_session_data *sd,int index,int amount)
- {
- struct guild_storage *stor;
- int flag;
- nullpo_retr(0, sd);
- nullpo_retr(0, stor=guild2storage2(sd->status.guild_id));
- if(!stor->storage_status)
- return 0;
-
- if(index<0 || index>=MAX_GUILD_STORAGE)
- return 0;
- if(stor->storage_[index].nameid <= 0)
- return 0;
-
- if(amount < 1 || amount > stor->storage_[index].amount)
- return 0;
- if((flag = pc_additem(sd,&stor->storage_[index],amount)) == 0)
- guild_storage_delitem(sd,stor,index,amount);
- else
- clif_additem(sd,0,0,flag);
- // log_fromstorage(sd, index, 1);
- return 0;
- }
- int storage_guild_storageaddfromcart(struct map_session_data *sd,int index,int amount)
- {
- struct guild_storage *stor;
- nullpo_retr(0, sd);
- nullpo_retr(0, stor=guild2storage2(sd->status.guild_id));
- if(!stor->storage_status || stor->storage_amount > MAX_GUILD_STORAGE)
- return 0;
- if(index<0 || index>=MAX_CART)
- return 0;
- if(sd->status.cart[index].nameid <= 0)
- return 0;
-
- if(amount < 1 || amount > sd->status.cart[index].amount)
- return 0;
- if(guild_storage_additem(sd,stor,&sd->status.cart[index],amount)==0)
- pc_cart_delitem(sd,index,amount,0);
- return 1;
- }
- int storage_guild_storagegettocart(struct map_session_data *sd,int index,int amount)
- {
- struct guild_storage *stor;
- nullpo_retr(0, sd);
- nullpo_retr(0, stor=guild2storage2(sd->status.guild_id));
- if(!stor->storage_status)
- return 0;
- if(index<0 || index>=MAX_GUILD_STORAGE)
- return 0;
-
- if(stor->storage_[index].nameid<=0)
- return 0;
-
- if(amount < 1 || amount > stor->storage_[index].amount)
- return 0;
- if(pc_cart_additem(sd,&stor->storage_[index],amount)==0)
- guild_storage_delitem(sd,stor,index,amount);
- return 1;
- }
- int storage_guild_storagesave(int account_id, int guild_id, int flag)
- {
- struct guild_storage *stor = guild2storage2(guild_id);
- if(stor)
- {
- if (flag) //Char quitting, close it.
- stor->storage_status = 0;
- if (stor->dirty)
- intif_send_guild_storage(account_id,stor);
- return 1;
- }
- return 0;
- }
- int storage_guild_storagesaved(int guild_id)
- {
- struct guild_storage *stor;
- if((stor=guild2storage2(guild_id)) != NULL) {
- if (stor->dirty && stor->storage_status == 0)
- { //Storage has been correctly saved.
- stor->dirty = 0;
- sortage_gsortitem(stor);
- }
- return 1;
- }
- return 0;
- }
- int storage_guild_storageclose(struct map_session_data *sd)
- {
- struct guild_storage *stor;
- nullpo_retr(0, sd);
- nullpo_retr(0, stor=guild2storage2(sd->status.guild_id));
- clif_storageclose(sd);
- if (stor->storage_status)
- {
- if (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);
- stor->storage_status=0;
- }
- sd->state.storage_flag = 0;
- return 0;
- }
- int storage_guild_storage_quit(struct map_session_data *sd,int flag)
- {
- struct guild_storage *stor;
- nullpo_retr(0, sd);
- nullpo_retr(0, stor=guild2storage2(sd->status.guild_id));
-
- 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 (save_settings&4)
- chrif_save(sd,0);
- return 0;
- }
- if(stor->storage_status) {
- if (save_settings&4)
- chrif_save(sd,0);
- else
- storage_guild_storagesave(sd->status.account_id,sd->status.guild_id,1);
- }
- sd->state.storage_flag = 0;
- stor->storage_status = 0;
- return 0;
- }
|