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