GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/being/flooritem.cpp Lines: 1 85 1.2 %
Date: 2017-11-29 Branches: 0 58 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "being/flooritem.h"
24
25
#include "configuration.h"
26
27
#include "render/graphics.h"
28
29
#include "gui/gui.h"
30
#include "gui/userpalette.h"
31
32
#include "gui/fonts/font.h"
33
34
#include "resources/iteminfo.h"
35
36
#include "resources/db/itemdb.h"
37
38
#include "resources/map/map.h"
39
40
#ifdef TMWA_SUPPORT
41
#include "net/net.h"
42
#endif  // TMWA_SUPPORT
43
#include "net/serverfeatures.h"
44
45
#include "debug.h"
46
47
extern volatile time_t cur_time;
48
49
FloorItem::FloorItem(const BeingId id,
50
                     const int itemId,
51
                     const int x, const int y,
52
                     const ItemTypeT itemType,
53
                     const int amount,
54
                     const int refine,
55
                     const ItemColor color,
56
                     const Identified identified,
57
                     const Damaged damaged,
58
                     const int *const cards) :
59
    ActorSprite(id),
60
    mCards(),
61
    mItemId(itemId),
62
    mX(x),
63
    mY(y),
64
    mDropTime(cur_time),
65
    mAmount(amount),
66
    mRefine(refine),
67
    mHeightPosDiff(0),
68
    mItemType(itemType),
69
    mPickupCount(0),
70
    mCursor(Cursor::CURSOR_PICKUP),
71
    mColor(color),
72
    mIdentified(identified),
73
    mDamaged(damaged),
74
    mShowMsg(true),
75
    mHighlight(config.getBoolValue("floorItemsHighlight"))
76
{
77
    setCards(cards, maxCards);
78
}
79
80
void FloorItem::postInit(Map *const map, int subX, int subY)
81
{
82
    setMap(map);
83
    const ItemInfo &info = ItemDB::get(mItemId);
84
    if (map != nullptr)
85
    {
86
        const int maxX = info.getMaxFloorOffsetX();
87
        const int maxY = info.getMaxFloorOffsetY();
88
89
        if (!serverFeatures->haveExtendedDropsPosition())
90
        {
91
            if (subX > maxX)
92
                subX = maxX;
93
            else if (subX < -maxX)
94
                subX = -maxX;
95
            if (subY > maxY)
96
                subY = maxY;
97
            else if (subY < -maxY)
98
                subY = -maxY;
99
100
            subX -= 8;
101
            subY -= 8;
102
        }
103
104
        mHeightPosDiff = map->getHeightOffset(mX, mY) * 16;
105
        mPixelX = mX * map->getTileWidth()
106
            + subX + mapTileSize / 2;
107
        mPixelY = mY * map->getTileHeight()
108
            + subY + mapTileSize - mHeightPosDiff;
109
        mPos.x = static_cast<float>(mPixelX);
110
        mPos.y = static_cast<float>(mPixelY);
111
        mYDiff = 31 - mHeightPosDiff;
112
    }
113
    else
114
    {
115
        mPixelX = 0;
116
        mPixelY = 0;
117
        mPos.x = 0;
118
        mPos.y = 0;
119
        mYDiff = 31;
120
    }
121
122
    mCursor = info.getPickupCursor();
123
    setupSpriteDisplay(info.getDisplay(),
124
        ForceDisplay_true,
125
        DisplayType::Floor,
126
        info.getDyeIconColorsString(mColor));
127
}
128
129
void FloorItem::setCards(const int *const cards,
130
                         int sz)
131
{
132
    if (sz < 0 || cards == nullptr)
133
        return;
134
    if (sz > maxCards)
135
        sz = maxCards;
136
    for (int f = 0; f < sz; f ++)
137
        mCards[f] = cards[f];
138
}
139
140
const ItemInfo &FloorItem::getInfo() const
141
{
142
    return ItemDB::get(mItemId);
143
}
144
145
std::string FloorItem::getName() const
146
{
147
    const ItemInfo &info = ItemDB::get(mItemId);
148
#ifdef TMWA_SUPPORT
149
    if (Net::getNetworkType() == ServerType::TMWATHENA)
150
    {
151
        return info.getName();
152
    }
153
#endif  // TMWA_SUPPORT
154
155
    return info.getName(mColor);
156
}
157
158
void FloorItem::draw(Graphics *const graphics,
159
                     const int offsetX, const int offsetY) const
160
{
161
    if (mMap == nullptr)
162
        return;
163
164
    BLOCK_START("FloorItem::draw")
165
    const int x = mX * mMap->getTileWidth() + offsetX;
166
    const int y = mY * mMap->getTileHeight() + offsetY - mHeightPosDiff;
167
    Font *font = nullptr;
168
169
    if (mHighlight)
170
    {
171
        const time_t curTime = cur_time;
172
        font = gui->getFont();
173
        if (mDropTime < curTime)
174
        {
175
            const int dx = mapTileSize;
176
            const int dy = mapTileSize;
177
178
            if (curTime > mDropTime + 28 && curTime < mDropTime + 50)
179
            {
180
                graphics->setColor(Color(80, 200, 20, 200));
181
                graphics->fillRectangle(Rect(
182
                                        x, y, dx, dy));
183
            }
184
            else if (curTime > mDropTime + 19
185
                     && curTime < mDropTime + 28)
186
            {
187
                graphics->setColor(Color(200, 80, 20,
188
                    80 + 10 * CAST_S32(curTime - mDropTime - 18)));
189
                graphics->fillRectangle(Rect(
190
                    x, y, dx, dy));
191
            }
192
            else if (curTime > mDropTime && curTime < mDropTime + 20)
193
            {
194
                graphics->setColor(Color(20, 20, 255,
195
                    7 * CAST_S32(curTime - mDropTime)));
196
                graphics->fillRectangle(Rect(x, y, dx, dy));
197
            }
198
        }
199
    }
200
201
    const int px = getActorX() + offsetX;
202
    const int py = getActorY() + offsetY;
203
    CompoundSprite::drawSimple(graphics, px, py);
204
205
    if (mHighlight)
206
    {
207
        if (font != nullptr && mAmount > 1)
208
        {
209
            const Color &color = userPalette->getColor(
210
                UserColorId::FLOOR_ITEM_TEXT);
211
            font->drawString(graphics,
212
                color, color,
213
                toString(mAmount),
214
                x, y);
215
        }
216
    }
217
    BLOCK_END("FloorItem::draw")
218
}
219
220
int FloorItem::getCard(const int index) const
221
{
222
    if (index < 0 || index >= maxCards)
223
        return 0;
224
    return mCards[index];
225
4
}