ManaPlus
flooritem.cpp
Go to the documentation of this file.
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 
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 
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();
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 
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
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(
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 }
const Color & getColor(UserColorIdT type, const unsigned int alpha=255U)
Definition: userpalette.h:159
const ItemInfo & get(const int id)
Definition: itemdb.cpp:814
void setupSpriteDisplay(const SpriteDisplay &display, const ForceDisplay forceDisplay, const DisplayTypeT displayType, const std::string &color)
void postInit(Map *const map, int subX, int subY)
Definition: flooritem.cpp:80
Gui * gui
Definition: gui.cpp:110
Definition: font.h:88
int getTileHeight() const
Definition: map.h:180
Definition: rect.h:72
CursorT getPickupCursor() const
Definition: iteminfo.h:294
#define BLOCK_START(name)
Definition: perfomance.h:78
Vector mPos
Definition: actor.h:139
Configuration config
void drawSimple(Graphics *const graphics, const int posX, const int posY) const
int BeingId
Definition: beingid.h:29
Definition: cursor.h:26
bool mHighlight
Definition: flooritem.h:150
ItemColor mColor
Definition: flooritem.h:146
#define BLOCK_END(name)
Definition: perfomance.h:79
Font * getFont() const
Definition: gui.h:159
uint16_t ItemColor
Definition: itemcolor.h:29
int getMaxFloorOffsetX() const
Definition: iteminfo.h:240
const std::string & getName() const
Definition: iteminfo.h:73
UserPalette * userPalette
Definition: userpalette.cpp:33
const bool ForceDisplay_true
Definition: forcedisplay.h:29
std::string getName() const
Definition: flooritem.cpp:145
int getActorY() const
Definition: actorsprite.h:162
Net::ServerFeatures * serverFeatures
Definition: net.cpp:94
int mPixelX
Definition: actor.h:132
virtual void setColor(const Color &color)
Definition: graphics.h:319
#define CAST_S32
Definition: cast.h:29
bool info(InputEvent &event)
Definition: commands.cpp:56
int getMaxFloorOffsetY() const
Definition: iteminfo.h:243
void drawString(Graphics *const graphics, Color col, const Color &col2, const std::string &text, const int x, const int y)
Definition: font.cpp:253
FloorItem(const BeingId id, const int itemId, const int x, const int y, const ItemTypeT itemType, const int amount, const int refine, const ItemColor color, const Identified identified, const Damaged damaged, const int *const cards)
Definition: flooritem.cpp:49
const SpriteDisplay & getDisplay() const
Definition: iteminfo.h:93
int mItemId
Definition: flooritem.h:137
int mPixelY
Definition: actor.h:133
uint8_t getHeightOffset(const int x, const int y) const
Definition: map.cpp:1612
std::string getDyeIconColorsString(const ItemColor color) const
Definition: iteminfo.cpp:239
int mCards[4]
Definition: flooritem.h:136
Definition: map.h:68
float x
Definition: vector.h:208
time_t mDropTime
Definition: flooritem.h:139
int getActorX() const
Definition: actorsprite.h:159
int mYDiff
Definition: actor.h:140
ItemType ::T ItemTypeT
Definition: itemtype.h:42
ServerTypeT getNetworkType()
Definition: net.cpp:178
volatile time_t cur_time
Definition: timer.cpp:57
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
virtual void fillRectangle(const Rect &rectangle)=0
CursorT mCursor
Definition: flooritem.h:145
int getTileWidth() const
Definition: map.h:174
float y
Definition: vector.h:208
int mAmount
Definition: flooritem.h:140
static const int mapTileSize
Definition: map.h:26
void draw(Graphics *const graphics, const int offsetX, const int offsetY) const
Definition: flooritem.cpp:158
Definition: color.h:74
void setMap(Map *const map)
const ItemInfo & getInfo() const
Definition: flooritem.cpp:140
void setCards(const int *const cards, int sz)
Definition: flooritem.cpp:129
int mHeightPosDiff
Definition: flooritem.h:142
#define maxCards
Definition: cards.h:24
virtual bool haveExtendedDropsPosition() const =0
int getCard(const int index) const
Definition: flooritem.cpp:220
Map * mMap
Definition: actor.h:138