thing.cpp

main 115 lines · 3 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1/*
2 * Copyright (c) 2010-2017 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"
26#include <framework/graphics/graphics.h>
27#include "map.h"
28#include "tile.h"
29#include "game.h"
30#include <framework/util/stats.h>
31
32Thing::Thing() :
33 m_datId(0)
34{
35 g_stats.addThing();
36}
37
38Thing::~Thing()
39{
40 g_stats.removeThing();
41}
42
43void Thing::setPosition(const Position& position)
44{
45 if(m_position == position)
46 return;
47
48 Position oldPos = m_position;
49 m_position = position;
50 onPositionChange(position, oldPos);
51}
52
53int Thing::getStackPriority()
54{
55 // bug fix for old versions
56 if (g_game.getClientVersion() <= 800 && isSplash()) {
57 return 1;
58 }
59 if(isGround())
60 return 0;
61 else if(isGroundBorder())
62 return 1;
63 else if(isOnBottom())
64 return 2;
65 else if(isOnTop())
66 return 3;
67 else if(isCreature())
68 return 4;
69 else // common items
70 return 5;
71}
72
73const TilePtr& Thing::getTile()
74{
75 return g_map.getTile(m_position);
76}
77
78ContainerPtr Thing::getParentContainer()
79{
80 if(m_position.x == 0xffff && m_position.y & 0x40) {
81 int containerId = m_position.y ^ 0x40;
82 return g_game.getContainer(containerId);
83 }
84 return nullptr;
85}
86
87int Thing::getStackPos()
88{
89 if(m_position.x == 65535 && isItem()) // is inside a container
90 return m_position.z;
91 else if(const TilePtr& tile = getTile())
92 return tile->getThingStackPos(static_self_cast<Thing>());
93 else {
94 g_logger.traceError("got a thing with invalid stackpos");
95 return -1;
96 }
97}
98
99const ThingTypePtr& Thing::getThingType()
100{
101 return g_things.getNullThingType();
102}
103
104ThingType* Thing::rawGetThingType()
105{
106 return g_things.getNullThingType().get();
107}
108
109Color Thing::updatedMarkedColor() {
110 if (!m_marked)
111 return Color::white;
112 m_markedColor.setAlpha(0.1f + std::abs(500 - g_clock.millis() % 1000) / 1000.0f); // 0.1-0.6
113 return m_markedColor;
114}
115
Top