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  *
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();
123  setupSpriteDisplay(info.getDisplay(),
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 
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  255U);
212  font->drawString(graphics,
213  color, color,
214  toString(mAmount),
215  x, y);
216  }
217  }
218  BLOCK_END("FloorItem::draw")
219 }
220 
221 int FloorItem::getCard(const int index) const
222 {
223  if (index < 0 || index >= maxCards)
224  return 0;
225  return mCards[index];
226 }
ForceDisplay_true
const bool ForceDisplay_true
Definition: forcedisplay.h:29
BeingId
int BeingId
Definition: beingid.h:29
Vector::x
float x
Definition: vector.h:208
flooritem.h
Actor::mMap
Map * mMap
Definition: actor.h:138
itemdb.h
Actor::mPos
Vector mPos
Definition: actor.h:139
FloorItem::setCards
void setCards(const int *const cards, int sz)
Definition: flooritem.cpp:129
FloorItem::mCards
int mCards[4]
Definition: flooritem.h:136
FloorItem::mCursor
CursorT mCursor
Definition: flooritem.h:145
UserColorId::FLOOR_ITEM_TEXT
@ FLOOR_ITEM_TEXT
Definition: usercolorid.h:92
ActorSprite::setupSpriteDisplay
void setupSpriteDisplay(const SpriteDisplay &display, const ForceDisplay forceDisplay, const DisplayTypeT displayType, const std::string &color)
Definition: actorsprite.cpp:364
CompoundSprite::drawSimple
void drawSimple(Graphics *const graphics, const int posX, const int posY) const
Definition: compoundsprite.cpp:148
FloorItem::mColor
ItemColor mColor
Definition: flooritem.h:146
userpalette.h
ItemTypeT
ItemType ::T ItemTypeT
Definition: itemtype.h:42
Graphics
Definition: graphics.h:107
iteminfo.h
FloorItem::draw
void draw(Graphics *const graphics, const int offsetX, const int offsetY) const
Definition: flooritem.cpp:158
bool
userPalette
UserPalette * userPalette
Definition: userpalette.cpp:33
Catch::toString
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
Gui::getFont
Font * getFont() const
Definition: gui.h:159
serverFeatures
Net::ServerFeatures * serverFeatures
Definition: net.cpp:100
Actions::info
bool info(InputEvent &event)
Definition: commands.cpp:56
FloorItem::getCard
int getCard(const int index) const
Definition: flooritem.cpp:221
ActorSprite::getActorY
int getActorY() const
Definition: actorsprite.h:162
Font::drawString
void drawString(Graphics *const graphics, Color col, const Color &col2, const std::string &text, const int x, const int y)
Definition: font.cpp:253
Actor::mPixelY
int mPixelY
Definition: actor.h:133
Map::getHeightOffset
uint8_t getHeightOffset(const int x, const int y) const
Definition: map.cpp:1630
FloorItem::mDropTime
time_t mDropTime
Definition: flooritem.h:139
FloorItem::mHeightPosDiff
int mHeightPosDiff
Definition: flooritem.h:142
font.h
Map::getTileWidth
int getTileWidth() const
Definition: map.h:177
gui
Gui * gui
Definition: gui.cpp:110
ItemInfo
Definition: iteminfo.h:52
FloorItem::postInit
void postInit(Map *const map, int subX, int subY)
Definition: flooritem.cpp:80
ActorSprite
Definition: actorsprite.h:54
Actor::mPixelX
int mPixelX
Definition: actor.h:132
x
x
Definition: graphics_calcImageRect.hpp:72
Font
Definition: font.h:88
DisplayType::Floor
@ Floor
Definition: displaytype.h:29
FloorItem::mItemId
int mItemId
Definition: flooritem.h:137
Actor::mYDiff
int mYDiff
Definition: actor.h:140
FloorItem::mAmount
int mAmount
Definition: flooritem.h:140
Net::ServerFeatures::haveExtendedDropsPosition
virtual bool haveExtendedDropsPosition() const =0
ItemColor
uint16_t ItemColor
Definition: itemcolor.h:29
config
Configuration config
Definition: configuration.cpp:51
maxCards
#define maxCards
Definition: cards.h:24
FloorItem::mHighlight
bool mHighlight
Definition: flooritem.h:150
Graphics::setColor
virtual void setColor(const Color &color)
Definition: graphics.h:319
gui.h
Rect
Definition: rect.h:72
Net::getNetworkType
ServerTypeT getNetworkType()
Definition: net.cpp:188
FloorItem::mY
int mY
Definition: flooritem.h:138
configuration.h
UserPalette::getColor
const Color & getColor(UserColorIdT type, const unsigned int alpha)
Definition: userpalette.h:159
ServerType::TMWATHENA
@ TMWATHENA
Definition: servertype.h:31
Map
Definition: map.h:71
Vector::y
float y
Definition: vector.h:208
Cursor::CURSOR_PICKUP
@ CURSOR_PICKUP
Definition: cursor.h:34
Map::getTileHeight
int getTileHeight() const
Definition: map.h:183
serverfeatures.h
ActorSprite::getActorX
int getActorX() const
Definition: actorsprite.h:159
BLOCK_START
#define BLOCK_START(name)
Definition: perfomance.h:78
BLOCK_END
#define BLOCK_END(name)
Definition: perfomance.h:79
cur_time
volatile time_t cur_time
Definition: timer.cpp:57
CAST_S32
#define CAST_S32
Definition: cast.h:29
ItemDB::get
const ItemInfo & get(const int id)
Definition: itemdb.cpp:792
Graphics::fillRectangle
virtual void fillRectangle(const Rect &rectangle)=0
ActorSprite::setMap
void setMap(Map *const map)
Definition: actorsprite.cpp:138
Color
Definition: color.h:74
FloorItem::getInfo
const ItemInfo & getInfo() const
Definition: flooritem.cpp:140
y
y
Definition: graphics_calcImageRect.hpp:72
FloorItem::getName
std::string getName() const
Definition: flooritem.cpp:145
FloorItem::mX
int mX
Definition: flooritem.h:138
debug.h
Cursor
Definition: cursor.h:26
mapTileSize
static const int mapTileSize
Definition: map.h:26
FloorItem::FloorItem
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