From 862e8f821789014b6167f37976b80694d6d310e3 Mon Sep 17 00:00:00 2001
From: Andrei Karas <akaras@inbox.ru>
Date: Fri, 21 Apr 2017 04:44:53 +0300
Subject: Add item options into item and get it from server.

---
 src/resources/inventory/inventory.cpp |  14 +++++
 src/resources/inventory/inventory.h   |   5 ++
 src/resources/item/item.cpp           |  11 ++++
 src/resources/item/item.h             |   8 +++
 src/resources/item/itemoption.h       |  32 +++++++++++
 src/resources/item/itemoptionslist.h  | 100 ++++++++++++++++++++++++++++++++++
 6 files changed, 170 insertions(+)
 create mode 100644 src/resources/item/itemoption.h
 create mode 100644 src/resources/item/itemoptionslist.h

(limited to 'src/resources')

diff --git a/src/resources/inventory/inventory.cpp b/src/resources/inventory/inventory.cpp
index 0f34ea977..ddc9acc1a 100644
--- a/src/resources/inventory/inventory.cpp
+++ b/src/resources/inventory/inventory.cpp
@@ -31,6 +31,7 @@
 #include "resources/iteminfo.h"
 
 #include "resources/item/item.h"
+#include "resources/item/itemoptionslist.h"
 
 #include "listeners/inventorylistener.h"
 
@@ -204,6 +205,19 @@ void Inventory::setCards(const int index,
         item1->setCards(cards, size);
 }
 
+void Inventory::setOptions(const int index,
+                           const ItemOptionsList *const options)
+{
+    if (index < 0 || index >= CAST_S32(mSize))
+    {
+        logger->log("Warning: invalid inventory index: %d", index);
+        return;
+    }
+    Item *const item1 = mItems[index];
+    if (item1)
+        item1->setOptions(options);
+}
+
 void Inventory::clear()
 {
     for (unsigned i = 0; i < mSize; i++)
diff --git a/src/resources/inventory/inventory.h b/src/resources/inventory/inventory.h
index f4d275eae..5180e49e1 100644
--- a/src/resources/inventory/inventory.h
+++ b/src/resources/inventory/inventory.h
@@ -47,6 +47,8 @@
 class InventoryListener;
 class Item;
 
+struct ItemOptionsList;
+
 class Inventory notfinal
 {
     public:
@@ -121,6 +123,9 @@ class Inventory notfinal
                       const int *const cards,
                       const int size) const;
 
+        void setOptions(const int index,
+                        const ItemOptionsList *const options);
+
         void moveItem(const int index1,
                       const int index2);
 
diff --git a/src/resources/item/item.cpp b/src/resources/item/item.cpp
index d738ca0e3..bc6614e52 100644
--- a/src/resources/item/item.cpp
+++ b/src/resources/item/item.cpp
@@ -30,10 +30,14 @@
 
 #include "resources/iteminfo.h"
 
+#include "resources/item/itemoptionslist.h"
+
 #include "resources/loaders/imageloader.h"
 
 #include "net/serverfeatures.h"
 
+#include "utils/delete2.h"
+
 #include "debug.h"
 
 DragDrop dragDrop(nullptr, DragDropSource::Empty);
@@ -56,6 +60,7 @@ Item::Item(const int id,
     mDescription(),
     mTags(),
     mCards(),
+    mOptions(nullptr),
     mRefine(refine),
     mInvIndex(0),
     mType(type),
@@ -78,6 +83,7 @@ Item::~Item()
         mImage->decRef();
         mImage = nullptr;
     }
+    delete2(mOptions);
     dragDrop.clearItem(this);
 }
 
@@ -176,6 +182,11 @@ void Item::addCard(const int card)
     }
 }
 
+void Item::setOptions(const ItemOptionsList *const options)
+{
+    mOptions = ItemOptionsList::copy(options);
+}
+
 void Item::updateColor()
 {
     if (serverFeatures && serverFeatures->haveItemColors())
diff --git a/src/resources/item/item.h b/src/resources/item/item.h
index 2a638f68a..f1712d26a 100644
--- a/src/resources/item/item.h
+++ b/src/resources/item/item.h
@@ -40,6 +40,8 @@
 
 class Image;
 
+struct ItemOptionsList;
+
 /**
  * Represents one or more instances of a certain item type.
  */
@@ -211,6 +213,11 @@ class Item notfinal
         const int *getCards() const noexcept2 A_WARN_UNUSED
         { return mCards; }
 
+        void setOptions(const ItemOptionsList *const options);
+
+        const ItemOptionsList *getOptions() const noexcept2 A_WARN_UNUSED
+        { return mOptions; }
+
         void setType(const ItemTypeT type) noexcept2
         { mType = type; }
 
@@ -245,6 +252,7 @@ class Item notfinal
         std::string mDescription;
         std::map <int, int> mTags;
         int mCards[maxCards];
+        const ItemOptionsList *mOptions;
         uint8_t mRefine;      /**< Item refine level. */
         int mInvIndex;        /**< Inventory index. */
         ItemTypeT mType;      /**< Item type. */
diff --git a/src/resources/item/itemoption.h b/src/resources/item/itemoption.h
new file mode 100644
index 000000000..9d3745f28
--- /dev/null
+++ b/src/resources/item/itemoption.h
@@ -0,0 +1,32 @@
+/*
+ *  The ManaPlus Client
+ *  Copyright (C) 2011-2017  The ManaPlus Developers
+ *
+ *  This file is part of The ManaPlus Client.
+ *
+ *  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 2 of the License, or
+ *  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/>.
+ */
+
+#ifndef RESOURCES_ITEM_ITEMOPTION_H
+#define RESOURCES_ITEM_ITEMOPTION_H
+
+#include "localconsts.h"
+
+struct ItemOption final
+{
+    uint16_t index;
+    int16_t value;
+};
+
+#endif  // RESOURCES_ITEM_ITEMOPTION_H
diff --git a/src/resources/item/itemoptionslist.h b/src/resources/item/itemoptionslist.h
new file mode 100644
index 000000000..7bb259028
--- /dev/null
+++ b/src/resources/item/itemoptionslist.h
@@ -0,0 +1,100 @@
+/*
+ *  The ManaPlus Client
+ *  Copyright (C) 2004-2009  The Mana World Development Team
+ *  Copyright (C) 2009-2010  The Mana Developers
+ *  Copyright (C) 2011-2017  The ManaPlus Developers
+ *
+ *  This file is part of The ManaPlus Client.
+ *
+ *  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 2 of the License, or
+ *  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/>.
+ */
+
+#ifndef RESOURCES_ITEM_ITEMOPTIONSLIST_H
+#define RESOURCES_ITEM_ITEMOPTIONSLIST_H
+
+#include "const/resources/item/itemoptions.h"
+
+#include "resources/item/itemoption.h"
+
+#include "localconsts.h"
+
+struct ItemOptionsList final
+{
+    explicit ItemOptionsList(const int amount0) :
+        amount(amount0),
+        pointer(0U)
+    {
+        options = new ItemOption[amount];
+    }
+
+    ItemOptionsList() :
+        amount(maxItemOptions),
+        pointer(0U)
+    {
+        options = new ItemOption[amount];
+    }
+
+    A_DEFAULT_COPY(ItemOptionsList)
+
+    ~ItemOptionsList()
+    {
+        delete [] options;
+        options = nullptr;
+    }
+
+    void add(const uint16_t index,
+             const uint16_t value)
+    {
+        if (pointer >= amount)
+            return;
+        options[pointer].index = index;
+        options[pointer].value = value;
+        pointer ++;
+    }
+
+    static ItemOptionsList *copy(const ItemOptionsList *const options0)
+    {
+        if (options0 == nullptr)
+            return nullptr;
+
+        const int amount0 = options0->amount;
+        ItemOptionsList *const obj = new ItemOptionsList(amount0);
+        for (int f = 0; f < amount0; f ++)
+        {
+            obj->options[f].index = options0->options[f].index;
+            obj->options[f].value = options0->options[f].value;
+        }
+        obj->amount = options0->amount;
+        obj->pointer = options0->pointer;
+        return obj;
+    }
+
+    size_t size() const
+    {
+        if (pointer < amount)
+            return pointer;
+        return amount;
+    }
+
+    const ItemOption &get(const size_t index) const
+    {
+        return options[index];
+    }
+
+    ItemOption *options;
+    size_t amount;
+    size_t pointer;
+};
+
+#endif  // RESOURCES_ITEM_ITEMOPTIONSLIST_H
-- 
cgit v1.2.3-70-g09d2