summaryrefslogtreecommitdiff
path: root/src/map/storage.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/map/storage.cpp')
-rw-r--r--src/map/storage.cpp120
1 files changed, 72 insertions, 48 deletions
diff --git a/src/map/storage.cpp b/src/map/storage.cpp
index 89357b9..a6e6a0d 100644
--- a/src/map/storage.cpp
+++ b/src/map/storage.cpp
@@ -1,14 +1,32 @@
#include "storage.hpp"
-// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
-// For more information, see COPYING in the main folder
-
-#include <cstdlib>
-#include <cstring>
+// storage.cpp - Storage handling.
+//
+// Copyright © ????-2004 Athena Dev Teams
+// Copyright © 2004-2011 The Mana World Development Team
+// Copyright © 2011-2014 Ben Longbons <b.r.longbons@gmail.com>
+//
+// This file is part of The Mana World (Athena server)
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "../compat/nullpo.hpp"
#include "../generic/db.hpp"
+#include "../mmo/ids.hpp"
+#include "../mmo/mmo.hpp"
+
#include "chrif.hpp"
#include "clif.hpp"
#include "intif.hpp"
@@ -18,18 +36,21 @@
#include "../poison.hpp"
+
+namespace tmwa
+{
static
-Map<int, struct storage> storage_db;
+Map<AccountId, Storage> storage_db;
void do_final_storage(void)
{
storage_db.clear();
}
-struct storage *account2storage(int account_id)
+Storage *account2storage(AccountId account_id)
{
- struct storage *stor = storage_db.search(account_id);
- if (stor == NULL)
+ Storage *stor = storage_db.search(account_id);
+ if (stor == nullptr)
{
stor = storage_db.init(account_id);
stor->account_id = account_id;
@@ -38,13 +59,13 @@ struct storage *account2storage(int account_id)
}
// Just to ask storage, without creation
-struct storage *account2storage2(int account_id)
+Storage *account2storage2(AccountId account_id)
{
return storage_db.search(account_id);
}
static
-void storage_delete(int account_id)
+void storage_delete(AccountId account_id)
{
storage_db.erase(account_id);
}
@@ -55,13 +76,13 @@ void storage_delete(int account_id)
*/
int storage_storageopen(dumb_ptr<map_session_data> sd)
{
- nullpo_ret(sd);
+ nullpo_retz(sd);
if (sd->state.storage_open)
return 1; //Already open?
- struct storage *stor = storage_db.search(sd->status_key.account_id);
- if (stor == NULL)
+ Storage *stor = storage_db.search(sd->status_key.account_id);
+ if (stor == nullptr)
{ //Request storage.
intif_request_storage(sd->status_key.account_id);
return 1;
@@ -83,20 +104,19 @@ int storage_storageopen(dumb_ptr<map_session_data> sd)
*------------------------------------------
*/
static
-int storage_additem(dumb_ptr<map_session_data> sd, struct storage *stor,
- struct item *item_data, int amount)
+int storage_additem(dumb_ptr<map_session_data> sd, Storage *stor,
+ Item *item_data, int amount)
{
struct item_data *data;
- int i;
- if (item_data->nameid <= 0 || amount <= 0)
+ if (!item_data->nameid || amount <= 0)
return 1;
data = itemdb_search(item_data->nameid);
if (!itemdb_isequip2(data))
{ //Stackable
- for (i = 0; i < MAX_STORAGE; i++)
+ for (SOff0 i : SOff0::iter())
{
if (compare_item(&stor->storage_[i], item_data))
{
@@ -110,9 +130,12 @@ int storage_additem(dumb_ptr<map_session_data> sd, struct storage *stor,
}
}
//Add item
- for (i = 0; i < MAX_STORAGE && stor->storage_[i].nameid; i++);
-
- if (i >= MAX_STORAGE)
+ SOff0 i = *std::find_if(SOff0::iter().begin(), SOff0::iter().end(),
+ [&stor](SOff0 i_)
+ {
+ return !stor->storage_[i_].nameid;
+ });
+ if (!i.ok())
return 1;
stor->storage_[i] = *item_data;
@@ -129,17 +152,17 @@ int storage_additem(dumb_ptr<map_session_data> sd, struct storage *stor,
*------------------------------------------
*/
static
-int storage_delitem(dumb_ptr<map_session_data> sd, struct storage *stor,
- int n, int amount)
+int storage_delitem(dumb_ptr<map_session_data> sd, Storage *stor,
+ SOff0 n, int amount)
{
- if (stor->storage_[n].nameid == 0 || stor->storage_[n].amount < amount)
+ if (!stor->storage_[n].nameid || stor->storage_[n].amount < amount)
return 1;
stor->storage_[n].amount -= amount;
if (stor->storage_[n].amount == 0)
{
- stor->storage_[n] = item{};
+ stor->storage_[n] = Item{};
stor->storage_amount--;
clif_updatestorageamount(sd, stor);
}
@@ -153,21 +176,21 @@ int storage_delitem(dumb_ptr<map_session_data> sd, struct storage *stor,
* Add an item to the storage from the inventory.
*------------------------------------------
*/
-int storage_storageadd(dumb_ptr<map_session_data> sd, int index, int amount)
+int storage_storageadd(dumb_ptr<map_session_data> sd, IOff0 index, int amount)
{
- struct storage *stor;
+ Storage *stor;
- nullpo_ret(sd);
+ nullpo_retz(sd);
stor = account2storage2(sd->status_key.account_id);
- nullpo_ret(stor);
+ nullpo_retz(stor);
if ((stor->storage_amount > MAX_STORAGE) || !stor->storage_status)
return 0; // storage full / storage closed
- if (index < 0 || index >= MAX_INVENTORY)
+ if (!index.ok())
return 0;
- if (sd->status.inventory[index].nameid <= 0)
+ if (!sd->status.inventory[index].nameid)
return 0; //No item on that spot
if (amount < 1 || amount > sd->status.inventory[index].amount)
@@ -188,19 +211,19 @@ int storage_storageadd(dumb_ptr<map_session_data> sd, int index, int amount)
* Retrieve an item from the storage.
*------------------------------------------
*/
-int storage_storageget(dumb_ptr<map_session_data> sd, int index, int amount)
+int storage_storageget(dumb_ptr<map_session_data> sd, SOff0 index, int amount)
{
- struct storage *stor;
+ Storage *stor;
PickupFail flag;
- nullpo_ret(sd);
+ nullpo_retz(sd);
stor = account2storage2(sd->status_key.account_id);
- nullpo_ret(stor);
+ nullpo_retz(stor);
- if (index < 0 || index >= MAX_STORAGE)
+ if (!index.ok())
return 0;
- if (stor->storage_[index].nameid <= 0)
+ if (!stor->storage_[index].nameid)
return 0; //Nothing there
if (amount < 1 || amount > stor->storage_[index].amount)
@@ -209,7 +232,7 @@ int storage_storageget(dumb_ptr<map_session_data> sd, int index, int amount)
if ((flag = pc_additem(sd, &stor->storage_[index], amount)) == PickupFail::OKAY)
storage_delitem(sd, stor, index, amount);
else
- clif_additem(sd, 0, 0, flag);
+ clif_additem(sd, IOff0::from(0), 0, flag);
// log_fromstorage(sd, index, 0);
return 1;
}
@@ -220,11 +243,11 @@ int storage_storageget(dumb_ptr<map_session_data> sd, int index, int amount)
*/
int storage_storageclose(dumb_ptr<map_session_data> sd)
{
- struct storage *stor;
+ Storage *stor;
- nullpo_ret(sd);
+ nullpo_retz(sd);
stor = account2storage2(sd->status_key.account_id);
- nullpo_ret(stor);
+ nullpo_retz(stor);
clif_storageclose(sd);
if (stor->storage_status)
@@ -252,9 +275,9 @@ int storage_storageclose(dumb_ptr<map_session_data> sd)
*/
int storage_storage_quit(dumb_ptr<map_session_data> sd)
{
- struct storage *stor;
+ Storage *stor;
- nullpo_ret(sd);
+ nullpo_retz(sd);
stor = account2storage2(sd->status_key.account_id);
if (stor)
@@ -267,9 +290,9 @@ int storage_storage_quit(dumb_ptr<map_session_data> sd)
return 0;
}
-int storage_storage_save(int account_id, int final)
+int storage_storage_save(AccountId account_id, int final)
{
- struct storage *stor;
+ Storage *stor;
stor = account2storage2(account_id);
if (!stor)
@@ -295,9 +318,9 @@ int storage_storage_save(int account_id, int final)
}
//Ack from Char-server indicating the storage was saved. [Skotlex]
-int storage_storage_saved(int account_id)
+int storage_storage_saved(AccountId account_id)
{
- struct storage *stor = account2storage2(account_id);
+ Storage *stor = account2storage2(account_id);
if (stor)
{
@@ -311,3 +334,4 @@ int storage_storage_saved(int account_id)
}
return 0;
}
+} // namespace tmwa