Otclient  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 "spritemanager.h"
25 #include "thingtypemanager.h"
27 #include "map.h"
28 #include "tile.h"
29 #include "game.h"
30 
32  m_datId(0)
33 {
34 }
35 
36 void Thing::setPosition(const Position& position)
37 {
38  if(m_position == position)
39  return;
40 
41  Position oldPos = m_position;
42  m_position = position;
43  onPositionChange(position, oldPos);
44 }
45 
47 {
48  if(isGround())
49  return 0;
50  else if(isGroundBorder())
51  return 1;
52  else if(isOnBottom())
53  return 2;
54  else if(isOnTop())
55  return 3;
56  else if(isCreature())
57  return 4;
58  else // common items
59  return 5;
60 }
61 
63 {
64  return g_map.getTile(m_position);
65 }
66 
68 {
69  if(m_position.x == 0xffff && m_position.y & 0x40) {
70  int containerId = m_position.y ^ 0x40;
71  return g_game.getContainer(containerId);
72  }
73  return nullptr;
74 }
75 
77 {
78  if(m_position.x == 65535 && isItem()) // is inside a container
79  return m_position.z;
80  else if(const TilePtr& tile = getTile())
81  return tile->getThingStackPos(static_self_cast<Thing>());
82  else {
83  g_logger.traceError("got a thing with invalid stackpos");
84  return -1;
85  }
86 }
87 
89 {
90  return g_things.getNullThingType();
91 }
92 
94 {
95  return g_things.getNullThingType().get();
96 }
Thing::Thing
Thing()
Definition: thing.cpp:31
thingtypemanager.h
graphics.h
Thing::isOnBottom
bool isOnBottom()
Definition: thing.h:87
g_map
Map g_map
Definition: map.cpp:36
Thing::getTile
const TilePtr & getTile()
Definition: thing.cpp:62
Position::x
int x
Definition: position.h:243
Thing::getThingType
virtual const ThingTypePtr & getThingType()
Definition: thing.cpp:88
Thing::isOnTop
bool isOnTop()
Definition: thing.h:88
Map::getTile
const TilePtr & getTile(const Position &pos)
Definition: map.cpp:310
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:244
Thing::m_position
Position m_position
Definition: thing.h:131
Position::z
short z
Definition: position.h:245
Thing::onPositionChange
virtual void onPositionChange(const Position &, const Position &)
Definition: thing.h:126
Thing::isGroundBorder
bool isGroundBorder()
Definition: thing.h:86
g_logger
Logger g_logger
Definition: logger.cpp:35
spritemanager.h
Thing::isCreature
virtual bool isCreature()
Definition: thing.h:54
Position
Definition: position.h:33
map.h
ThingType
Definition: thingtype.h:123
Thing::rawGetThingType
virtual ThingType * rawGetThingType()
Definition: thing.cpp:93
Thing::getStackPos
int getStackPos()
Definition: thing.cpp:76
stdext::shared_object_ptr< Tile >
Thing::getStackPriority
int getStackPriority()
Definition: thing.cpp:46
game.h
Thing::getParentContainer
ContainerPtr getParentContainer()
Definition: thing.cpp:67
Thing::setPosition
void setPosition(const Position &position)
Definition: thing.cpp:36
thing.h
Game::getContainer
ContainerPtr getContainer(int index)
Definition: game.h:332
Thing::isItem
virtual bool isItem()
Definition: thing.h:51
Thing::isGround
bool isGround()
Definition: thing.h:85
tile.h
g_things
ThingTypeManager g_things
Definition: thingtypemanager.cpp:38
stdext::shared_object_ptr::get
T * get() const
Definition: shared_object.h:82