Otclient 1.0  14/8/2020
thing.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "thing.h"
24 #include "thing.h"
26 #include "game.h"
27 #include "map.h"
28 #include "spritemanager.h"
29 #include "thingtypemanager.h"
30 #include "tile.h"
31 
32 Thing::Thing() : m_datId(0) {}
33 
34 
35 void Thing::requestDrawing(const bool force)
36 {
37  const auto _isLocalPlayer = isLocalPlayer();
38  uint32_t redrawFlag;
39 
40  if(isStaticText()) redrawFlag = Otc::ReDrawStaticText;
41  else {
42  redrawFlag = Otc::ReDrawThing;
43 
44  if(isItem()) static_self_cast<Item>()->startListenerPainter();
45  else if(isCreature()) redrawFlag |= Otc::ReDrawAllInformation;
46 
47  if(_isLocalPlayer || hasLight()) redrawFlag |= Otc::ReDrawLight;
48  }
49 
50  g_map.requestDrawing(m_position, static_cast<Otc::RequestDrawFlags>(redrawFlag), force || _isLocalPlayer, _isLocalPlayer);
51 }
52 
53 void Thing::setPosition(const Position& position)
54 {
55  if(m_position == position)
56  return;
57 
58  const Position oldPos = m_position;
59  m_position = position;
60  onPositionChange(position, oldPos);
61 }
62 
64 {
65  if(isGround())
66  return 0;
67 
68  if(isGroundBorder())
69  return 1;
70 
71  if(isOnBottom())
72  return 2;
73 
74  if(isOnTop())
75  return 3;
76 
77  if(isCreature())
78  return 4;
79 
80  // common items
81  return 5;
82 }
83 
85 {
86  return g_map.getTile(m_position);
87 }
88 
90 {
91  if(m_position.x == 0xffff && m_position.y & 0x40) {
92  const int containerId = m_position.y ^ 0x40;
93  return g_game.getContainer(containerId);
94  }
95 
96  return nullptr;
97 }
98 
100 {
101  if(m_position.x == 65535 && isItem()) // is inside a container
102  return m_position.z;
103 
104  if(const TilePtr& tile = getTile())
105  return tile->getThingStackPos(static_self_cast<Thing>());
106 
107  g_logger.traceError("got a thing with invalid stackpos");
108  return -1;
109 }
110 
112 {
113  return g_things.getNullThingType();
114 }
115 
117 {
118  return g_things.getNullThingType().get();
119 }
Thing::Thing
Thing()
Definition: thing.cpp:32
thingtypemanager.h
Map::requestDrawing
void requestDrawing(const Position &pos, const Otc::RequestDrawFlags reDrawFlags, const bool force=false, const bool isLocalPlayer=false)
Definition: map.cpp:84
graphics.h
Thing::isOnBottom
bool isOnBottom()
Definition: thing.h:92
g_map
Map g_map
Definition: map.cpp:36
Otc::ReDrawStaticText
@ ReDrawStaticText
Definition: const.h:58
Thing::getTile
const TilePtr & getTile()
Definition: thing.cpp:84
Position::x
int x
Definition: position.h:265
Thing::getThingType
virtual const ThingTypePtr & getThingType()
Definition: thing.cpp:111
Thing::hasLight
virtual bool hasLight()
Definition: thing.h:85
Thing::isOnTop
bool isOnTop()
Definition: thing.h:93
Map::getTile
const TilePtr & getTile(const Position &pos)
Definition: map.cpp:358
Otc::ReDrawThing
@ ReDrawThing
Definition: const.h:56
ThingTypeManager::getNullThingType
const ThingTypePtr & getNullThingType()
Definition: thingtypemanager.h:52
g_game
Game g_game
Definition: game.cpp:37
Position::y
int y
Definition: position.h:266
Otc::ReDrawLight
@ ReDrawLight
Definition: const.h:57
Otc::RequestDrawFlags
RequestDrawFlags
Definition: const.h:55
Thing::requestDrawing
void requestDrawing(const bool force=false)
Definition: thing.cpp:35
Thing::m_position
Position m_position
Definition: thing.h:141
Position::z
short z
Definition: position.h:267
Thing::onPositionChange
virtual void onPositionChange(const Position &, const Position &)
Definition: thing.h:136
Thing::isGroundBorder
bool isGroundBorder()
Definition: thing.h:91
g_logger
Logger g_logger
Definition: logger.cpp:35
spritemanager.h
Thing::isCreature
virtual bool isCreature()
Definition: thing.h:55
Position
Definition: position.h:33
Thing::isLocalPlayer
virtual bool isLocalPlayer()
Definition: thing.h:59
map.h
ThingType
Definition: thingtype.h:126
Otc::ReDrawAllInformation
@ ReDrawAllInformation
Definition: const.h:64
Thing::rawGetThingType
virtual ThingType * rawGetThingType()
Definition: thing.cpp:116
Thing::getStackPos
int getStackPos()
Definition: thing.cpp:99
stdext::shared_object_ptr< Tile >
Thing::getStackPriority
int getStackPriority()
Definition: thing.cpp:63
game.h
Thing::isStaticText
virtual bool isStaticText()
Definition: thing.h:61
Thing::getParentContainer
ContainerPtr getParentContainer()
Definition: thing.cpp:89
Thing::setPosition
void setPosition(const Position &position)
Definition: thing.cpp:53
thing.h
Game::getContainer
ContainerPtr getContainer(int index)
Definition: game.h:334
Thing::isItem
virtual bool isItem()
Definition: thing.h:52
Thing::isGround
bool isGround()
Definition: thing.h:90
tile.h
g_things
ThingTypeManager g_things
Definition: thingtypemanager.cpp:38
stdext::shared_object_ptr::get
T * get() const
Definition: shared_object.h:82