summaryrefslogtreecommitdiff
path: root/src/map/storage.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/map/storage.c')
-rw-r--r--src/map/storage.c154
1 files changed, 82 insertions, 72 deletions
diff --git a/src/map/storage.c b/src/map/storage.c
index eb6ce0363..964d3008c 100644
--- a/src/map/storage.c
+++ b/src/map/storage.c
@@ -30,7 +30,7 @@ static DBMap* guild_storage_db; // int guild_id -> struct guild_storage*
/*==========================================
* 倉庫内アイテムソート
*------------------------------------------*/
-int storage_comp_item(const void *_i1, const void *_i2)
+static int storage_comp_item(const void *_i1, const void *_i2)
{
struct item *i1 = (struct item *)_i1;
struct item *i2 = (struct item *)_i2;
@@ -44,13 +44,13 @@ int storage_comp_item(const void *_i1, const void *_i2)
return i1->nameid - i2->nameid;
}
-void storage_sortitem (struct storage_data *stor)
+static void storage_sortitem (struct storage_data *stor)
{
nullpo_retv(stor);
qsort(stor->items, MAX_STORAGE, sizeof(struct item), storage_comp_item);
}
-void storage_gsortitem (struct guild_storage* gstor)
+static void storage_gsortitem (struct guild_storage* gstor)
{
nullpo_retv(gstor);
qsort(gstor->storage_, MAX_GUILD_STORAGE, sizeof(struct item), storage_comp_item);
@@ -90,7 +90,7 @@ static int storage_reconnect_sub(DBKey key,void *data,va_list ap)
return 0;
}
-//Function to be invoked upon server reconnection to char. To save all 'dirty' storages [Skotlex
+//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);
@@ -153,7 +153,7 @@ int storage_storageopen(struct map_session_data *sd)
stor->storage_status = 1;
sd->state.storage_flag = 1;
clif_storagelist(sd,stor);
- clif_updatestorageamount(sd,stor);
+ clif_updatestorageamount(sd,stor->storage_amount);
return 0;
}
@@ -175,29 +175,32 @@ int compare_item(struct item *a, struct item *b)
/*==========================================
* Internal add-item function.
*------------------------------------------*/
-static int storage_additem(struct map_session_data *sd,struct storage_data *stor,struct item *item_data,int amount)
+static int storage_additem(struct map_session_data* sd, struct storage_data* stor,struct item *item_data,int amount)
{
struct item_data *data;
int i;
- if(item_data->nameid <= 0 || amount <= 0)
+ if( item_data->nameid <= 0 || amount <= 0 )
return 1;
data = itemdb_search(item_data->nameid);
- if (!itemdb_canstore(item_data, pc_isGM(sd)))
+ 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->items[i], item_data)) {
- if(amount > MAX_AMOUNT - stor->items[i].amount)
+ if( itemdb_isstackable2(data) )
+ {//Stackable
+ for( i = 0; i < MAX_STORAGE; i++ )
+ {
+ if( compare_item (&stor->items[i], item_data))
+ {// existing items found, stack them
+ if( amount > MAX_AMOUNT - stor->items[i].amount )
return 1;
stor->items[i].amount+=amount;
- clif_storageitemadded(sd,stor,i,amount);
+ clif_storageitemadded(sd,&stor->items[i],i,amount);
stor->dirty = 1;
if(log_config.enable_logs&0x800)
log_pick_pc(sd, "R", item_data->nameid, -amount, item_data);
@@ -205,69 +208,73 @@ static int storage_additem(struct map_session_data *sd,struct storage_data *stor
}
}
}
- //Add item
- for(i=0;i<MAX_STORAGE && stor->items[i].nameid;i++);
-
- if(i>=MAX_STORAGE)
+
+ // find free slot
+ ARR_FIND( 0, MAX_STORAGE, i, stor->items[i].nameid == 0 );
+ if( i >= MAX_STORAGE )
return 1;
+ // add item to slot
memcpy(&stor->items[i],item_data,sizeof(stor->items[0]));
stor->items[i].amount=amount;
stor->storage_amount++;
- clif_storageitemadded(sd,stor,i,amount);
- clif_updatestorageamount(sd,stor);
+ clif_storageitemadded(sd,&stor->items[i],i,amount);
+ clif_updatestorageamount(sd,stor->storage_amount);
stor->dirty = 1;
if(log_config.enable_logs&0x800)
log_pick_pc(sd, "R", item_data->nameid, -amount, item_data);
+
return 0;
}
+
/*==========================================
* Internal del-item function
*------------------------------------------*/
-static int storage_delitem(struct map_session_data *sd,struct storage_data *stor,int n,int amount)
+static int storage_delitem(struct map_session_data* sd, struct storage_data* stor, int n, int amount)
{
-
- if(stor->items[n].nameid==0 || stor->items[n].amount<amount)
+ if( stor->items[n].nameid == 0 || stor->items[n].amount < amount )
return 1;
- stor->items[n].amount-=amount;
- if(log_config.enable_logs&0x800)
+ stor->items[n].amount -= amount;
+
+ if( log_config.enable_logs&0x800 )
log_pick_pc(sd, "R", stor->items[n].nameid, amount, &stor->items[n]);
- if(stor->items[n].amount==0){
+
+ if( stor->items[n].amount == 0 )
+ {
memset(&stor->items[n],0,sizeof(stor->items[0]));
stor->storage_amount--;
- clif_updatestorageamount(sd,stor);
+ clif_updatestorageamount(sd,stor->storage_amount);
}
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)
+int storage_storageadd(struct map_session_data* sd, int index, int amount)
{
struct storage_data *stor;
nullpo_retr(0, sd);
nullpo_retr(0, stor=account2storage2(sd->status.account_id));
- if((stor->storage_amount > MAX_STORAGE) || !stor->storage_status)
+ if( stor->storage_amount > MAX_STORAGE || !stor->storage_status )
return 0; // storage full / storage closed
- if(index<0 || index>=MAX_INVENTORY)
+ if( index < 0 || index >= MAX_INVENTORY )
return 0;
- if(sd->status.inventory[index].nameid <= 0)
+ if( sd->status.inventory[index].nameid <= 0 )
return 0; //No item on that spot
- if(amount < 1 || amount > sd->status.inventory[index].amount)
+ 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
+ if( storage_additem(sd,stor,&sd->status.inventory[index],amount) == 0 )
pc_delitem(sd,index,amount,0);
return 1;
@@ -276,7 +283,7 @@ int storage_storageadd(struct map_session_data *sd,int index,int amount)
/*==========================================
* Retrieve an item from the storage.
*------------------------------------------*/
-int storage_storageget(struct map_session_data *sd,int index,int amount)
+int storage_storageget(struct map_session_data* sd, int index, int amount)
{
struct storage_data *stor;
int flag;
@@ -285,45 +292,46 @@ int storage_storageget(struct map_session_data *sd,int index,int amount)
nullpo_retr(0, stor=account2storage2(sd->status.account_id));
- if(index<0 || index>=MAX_STORAGE)
+ if( index < 0 || index >= MAX_STORAGE )
return 0;
- if(stor->items[index].nameid <= 0)
+ if( stor->items[index].nameid <= 0 )
return 0; //Nothing there
- if(amount < 1 || amount > stor->items[index].amount)
+ if( amount < 1 || amount > stor->items[index].amount )
return 0;
- if((flag = pc_additem(sd,&stor->items[index],amount)) == 0)
+ if( (flag = pc_additem(sd,&stor->items[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)
+int storage_storageaddfromcart(struct map_session_data* sd, int index, int amount)
{
struct storage_data *stor;
nullpo_retr(0, sd);
nullpo_retr(0, stor=account2storage2(sd->status.account_id));
- if(stor->storage_amount > MAX_STORAGE || !stor->storage_status)
+ if( stor->storage_amount > MAX_STORAGE || !stor->storage_status )
return 0; // storage full / storage closed
- if(index< 0 || index>=MAX_CART)
+ if( index < 0 || index >= MAX_CART )
return 0;
- if(sd->status.cart[index].nameid <= 0)
+ if( sd->status.cart[index].nameid <= 0 )
return 0; //No item there.
- if(amount < 1 || amount > sd->status.cart[index].amount)
+ if( amount < 1 || amount > sd->status.cart[index].amount )
return 0;
- if(storage_additem(sd,stor,&sd->status.cart[index],amount)==0)
+ if( storage_additem(sd,stor,&sd->status.cart[index],amount) == 0 )
pc_cart_delitem(sd,index,amount,0);
return 1;
@@ -332,26 +340,26 @@ int storage_storageaddfromcart(struct map_session_data *sd,int index,int amount)
/*==========================================
* Get from Storage to the Cart
*------------------------------------------*/
-int storage_storagegettocart(struct map_session_data *sd,int index,int amount)
+int storage_storagegettocart(struct map_session_data* sd, int index, int amount)
{
struct storage_data *stor;
nullpo_retr(0, sd);
nullpo_retr(0, stor=account2storage2(sd->status.account_id));
- if(!stor->storage_status)
+ if( !stor->storage_status )
return 0;
- if(index< 0 || index>=MAX_STORAGE)
+ if( index < 0 || index >= MAX_STORAGE )
return 0;
- if(stor->items[index].nameid <= 0)
+ if( stor->items[index].nameid <= 0 )
return 0; //Nothing there.
- if(amount < 1 || amount > stor->items[index].amount)
+ if( amount < 1 || amount > stor->items[index].amount )
return 0;
- if(pc_cart_additem(sd,&stor->items[index],amount)==0)
+ if( pc_cart_additem(sd,&stor->items[index],amount) == 0 )
storage_delitem(sd,stor,index,amount);
return 1;
@@ -361,7 +369,7 @@ int storage_storagegettocart(struct map_session_data *sd,int index,int amount)
/*==========================================
* Modified By Valaris to save upon closing [massdriller]
*------------------------------------------*/
-int storage_storageclose(struct map_session_data *sd)
+int storage_storageclose(struct map_session_data* sd)
{
struct storage_data *stor;
@@ -376,22 +384,23 @@ int storage_storageclose(struct map_session_data *sd)
else
storage_storage_save(sd->status.account_id, 0);
}
- stor->storage_status=0;
- sd->state.storage_flag=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)
+int storage_storage_quit(struct map_session_data* sd, int flag)
{
struct storage_data *stor;
nullpo_retr(0, sd);
nullpo_retr(0, stor=account2storage2(sd->status.account_id));
- if (stor->storage_status)
+ if( stor->storage_status )
{
if (save_settings&4)
chrif_save(sd, flag); //Invokes the storage saving as well.
@@ -400,10 +409,11 @@ int storage_storage_quit(struct map_session_data *sd, int flag)
}
stor->storage_status = 0;
sd->state.storage_flag = 0;
+
return 0;
}
-void storage_storage_dirty(struct map_session_data *sd)
+void storage_storage_dirty(struct map_session_data* sd)
{
struct storage_data *stor;
@@ -487,7 +497,7 @@ int guild_storage_delete(int guild_id)
return 0;
}
-int storage_guild_storageopen(struct map_session_data *sd)
+int storage_guild_storageopen(struct map_session_data* sd)
{
struct guild_storage *gstor;
@@ -514,11 +524,11 @@ int storage_guild_storageopen(struct map_session_data *sd)
gstor->storage_status = 1;
sd->state.storage_flag = 2;
clif_guildstoragelist(sd,gstor);
- clif_updateguildstorageamount(sd,gstor);
+ clif_updateguildstorageamount(sd,gstor->storage_amount);
return 0;
}
-int guild_storage_additem(struct map_session_data *sd,struct guild_storage *stor,struct item *item_data,int amount)
+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;
@@ -543,7 +553,7 @@ int guild_storage_additem(struct map_session_data *sd,struct guild_storage *stor
if(stor->storage_[i].amount+amount > MAX_AMOUNT)
return 1;
stor->storage_[i].amount+=amount;
- clif_guildstorageitemadded(sd,stor,i,amount);
+ clif_guildstorageitemadded(sd,&stor->storage_[i],i,amount);
stor->dirty = 1;
if(log_config.enable_logs&0x1000)
log_pick_pc(sd, "G", item_data->nameid, -amount, item_data);
@@ -560,15 +570,15 @@ int guild_storage_additem(struct map_session_data *sd,struct guild_storage *stor
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);
+ clif_guildstorageitemadded(sd,&stor->storage_[i],i,amount);
+ clif_updateguildstorageamount(sd,stor->storage_amount);
stor->dirty = 1;
if(log_config.enable_logs&0x1000)
log_pick_pc(sd, "G", item_data->nameid, -amount, item_data);
return 0;
}
-int guild_storage_delitem(struct map_session_data *sd,struct guild_storage *stor,int n,int amount)
+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);
@@ -582,14 +592,14 @@ int guild_storage_delitem(struct map_session_data *sd,struct guild_storage *stor
if(stor->storage_[n].amount==0){
memset(&stor->storage_[n],0,sizeof(stor->storage_[0]));
stor->storage_amount--;
- clif_updateguildstorageamount(sd,stor);
+ clif_updateguildstorageamount(sd,stor->storage_amount);
}
clif_storageitemremoved(sd,n,amount);
stor->dirty = 1;
return 0;
}
-int storage_guild_storageadd(struct map_session_data *sd,int index,int amount)
+int storage_guild_storageadd(struct map_session_data* sd, int index, int amount)
{
struct guild_storage *stor;
@@ -615,7 +625,7 @@ int storage_guild_storageadd(struct map_session_data *sd,int index,int amount)
return 1;
}
-int storage_guild_storageget(struct map_session_data *sd,int index,int amount)
+int storage_guild_storageget(struct map_session_data* sd, int index, int amount)
{
struct guild_storage *stor;
int flag;
@@ -644,7 +654,7 @@ int storage_guild_storageget(struct map_session_data *sd,int index,int amount)
return 0;
}
-int storage_guild_storageaddfromcart(struct map_session_data *sd,int index,int amount)
+int storage_guild_storageaddfromcart(struct map_session_data* sd, int index, int amount)
{
struct guild_storage *stor;
@@ -669,7 +679,7 @@ int storage_guild_storageaddfromcart(struct map_session_data *sd,int index,int a
return 1;
}
-int storage_guild_storagegettocart(struct map_session_data *sd,int index,int amount)
+int storage_guild_storagegettocart(struct map_session_data* sd, int index, int amount)
{
struct guild_storage *stor;
@@ -724,7 +734,7 @@ int storage_guild_storagesaved(int guild_id)
return 0;
}
-int storage_guild_storageclose(struct map_session_data *sd)
+int storage_guild_storageclose(struct map_session_data* sd)
{
struct guild_storage *stor;
@@ -745,7 +755,7 @@ 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;