Otclient  14/8/2020
minimap.h
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 
24 #ifndef MINIMAP_H
25 #define MINIMAP_H
26 
27 #include "declarations.h"
29 
30 enum {
32  OTMM_SIGNATURE = 0x4D4d544F,
34 };
35 
40 };
41 
42 #pragma pack(push,1) // disable memory alignment
44 {
45  MinimapTile() : flags(0), color(255), speed(10) { }
49  bool hasFlag(MinimapTileFlags flag) const { return flags & flag; }
50  int getSpeed() const { return speed * 10; }
51  bool operator==(const MinimapTile& other) const { return color == other.color && flags == other.flags && speed == other.speed; }
52  bool operator!=(const MinimapTile& other) const { return !(*this == other); }
53 };
54 
56 {
57 public:
58  void clean();
59  void update();
60  void updateTile(int x, int y, const MinimapTile& tile);
61  MinimapTile& getTile(int x, int y) { return m_tiles[getTileIndex(x,y)]; }
62  void resetTile(int x, int y) { m_tiles[getTileIndex(x,y)] = MinimapTile(); }
63  uint getTileIndex(int x, int y) { return ((y % MMBLOCK_SIZE) * MMBLOCK_SIZE) + (x % MMBLOCK_SIZE); }
64  const TexturePtr& getTexture() { return m_texture; }
65  std::array<MinimapTile, MMBLOCK_SIZE *MMBLOCK_SIZE>& getTiles() { return m_tiles; }
66  void mustUpdate() { m_mustUpdate = true; }
67  void justSaw() { m_wasSeen = true; }
68  bool wasSeen() { return m_wasSeen; }
69 private:
70  TexturePtr m_texture;
71  std::array<MinimapTile, MMBLOCK_SIZE *MMBLOCK_SIZE> m_tiles;
72  stdext::boolean<true> m_mustUpdate;
73  stdext::boolean<false> m_wasSeen;
74 };
75 
76 #pragma pack(pop)
77 
78 class Minimap
79 {
80 
81 public:
82  void init();
83  void terminate();
84 
85  void clean();
86 
87  void draw(const Rect& screenRect, const Position& mapCenter, float scale, const Color& color);
88  Point getTilePoint(const Position& pos, const Rect& screenRect, const Position& mapCenter, float scale);
89  Position getTilePosition(const Point& point, const Rect& screenRect, const Position& mapCenter, float scale);
90  Rect getTileRect(const Position& pos, const Rect& screenRect, const Position& mapCenter, float scale);
91 
92  void updateTile(const Position& pos, const TilePtr& tile);
93  const MinimapTile& getTile(const Position& pos);
94 
95  bool loadImage(const std::string& fileName, const Position& topLeft, float colorFactor);
96  void saveImage(const std::string& fileName, const Rect& mapRect);
97  bool loadOtmm(const std::string& fileName);
98  void saveOtmm(const std::string& fileName);
99 
100 private:
101  Rect calcMapRect(const Rect& screenRect, const Position& mapCenter, float scale);
102  bool hasBlock(const Position& pos) { return m_tileBlocks[pos.z].find(getBlockIndex(pos)) != m_tileBlocks[pos.z].end(); }
103  MinimapBlock& getBlock(const Position& pos) { return m_tileBlocks[pos.z][getBlockIndex(pos)]; }
104  Point getBlockOffset(const Point& pos) { return Point(pos.x - pos.x % MMBLOCK_SIZE,
105  pos.y - pos.y % MMBLOCK_SIZE); }
106  Position getIndexPosition(int index, int z) { return Position((index % (65536 / MMBLOCK_SIZE))*MMBLOCK_SIZE,
107  (index / (65536 / MMBLOCK_SIZE))*MMBLOCK_SIZE, z); }
108  uint getBlockIndex(const Position& pos) { return ((pos.y / MMBLOCK_SIZE) * (65536 / MMBLOCK_SIZE)) + (pos.x / MMBLOCK_SIZE); }
109  std::unordered_map<uint, MinimapBlock> m_tileBlocks[Otc::MAX_Z+1];
110 };
111 
112 extern Minimap g_minimap;
113 
114 #endif
MinimapBlock::mustUpdate
void mustUpdate()
Definition: minimap.h:66
MinimapBlock::getTile
MinimapTile & getTile(int x, int y)
Definition: minimap.h:61
MinimapTileNotPathable
@ MinimapTileNotPathable
Definition: minimap.h:38
MinimapTile::MinimapTile
MinimapTile()
Definition: minimap.h:45
Color
Definition: color.h:32
MinimapBlock::resetTile
void resetTile(int x, int y)
Definition: minimap.h:62
Minimap::loadOtmm
bool loadOtmm(const std::string &fileName)
Definition: minimap.cpp:306
TPoint::y
T y
Definition: point.h:83
MinimapBlock::update
void update()
Definition: minimap.cpp:45
z
gc sort z
Definition: CMakeLists.txt:176
TRect< int >
Position::x
int x
Definition: position.h:243
Minimap::saveImage
void saveImage(const std::string &fileName, const Rect &mapRect)
Definition: minimap.cpp:301
Point
TPoint< int > Point
Definition: point.h:86
Position::y
int y
Definition: position.h:244
stdext::boolean< true >
MinimapTileNotWalkable
@ MinimapTileNotWalkable
Definition: minimap.h:39
MinimapTile::operator==
bool operator==(const MinimapTile &other) const
Definition: minimap.h:51
OTMM_VERSION
@ OTMM_VERSION
Definition: minimap.h:33
MMBLOCK_SIZE
@ MMBLOCK_SIZE
Definition: minimap.h:31
MinimapTile::color
uint8 color
Definition: minimap.h:47
MinimapTileFlags
MinimapTileFlags
Definition: minimap.h:36
MinimapBlock::clean
void clean()
Definition: minimap.cpp:38
Position::z
short z
Definition: position.h:245
OTMM_SIGNATURE
@ OTMM_SIGNATURE
Definition: minimap.h:32
Minimap::updateTile
void updateTile(const Position &pos, const TilePtr &tile)
Definition: minimap.cpp:193
Minimap::getTile
const MinimapTile & getTile(const Position &pos)
Definition: minimap.cpp:214
declarations.h
uint
unsigned int uint
Definition: types.h:31
MinimapTile::flags
uint8 flags
Definition: minimap.h:46
MinimapBlock::getTileIndex
uint getTileIndex(int x, int y)
Definition: minimap.h:63
Minimap::clean
void clean()
Definition: minimap.cpp:95
Position
Definition: position.h:33
MinimapTile::hasFlag
bool hasFlag(MinimapTileFlags flag) const
Definition: minimap.h:49
TPoint::x
T x
Definition: point.h:83
Otc::MAX_Z
@ MAX_Z
Definition: const.h:33
Minimap::terminate
void terminate()
Definition: minimap.cpp:90
Minimap::init
void init()
Definition: minimap.cpp:86
MinimapTile
Definition: minimap.h:43
Minimap::getTileRect
Rect getTileRect(const Position &pos, const Rect &screenRect, const Position &mapCenter, float scale)
Definition: minimap.cpp:174
Minimap::getTilePosition
Position getTilePosition(const Point &point, const Rect &screenRect, const Position &mapCenter, float scale)
Definition: minimap.cpp:163
g_minimap
Minimap g_minimap
Definition: minimap.cpp:36
Minimap::getTilePoint
Point getTilePoint(const Position &pos, const Rect &screenRect, const Position &mapCenter, float scale)
Definition: minimap.cpp:152
declarations.h
stdext::shared_object_ptr< Texture >
MinimapTile::speed
uint8 speed
Definition: minimap.h:48
MinimapTile::operator!=
bool operator!=(const MinimapTile &other) const
Definition: minimap.h:52
MinimapBlock::updateTile
void updateTile(int x, int y, const MinimapTile &tile)
Definition: minimap.cpp:78
MinimapTile::getSpeed
int getSpeed() const
Definition: minimap.h:50
MinimapBlock::getTexture
const TexturePtr & getTexture()
Definition: minimap.h:64
MinimapBlock
Definition: minimap.h:55
MinimapTileWasSeen
@ MinimapTileWasSeen
Definition: minimap.h:37
Minimap
Definition: minimap.h:78
Minimap::saveOtmm
void saveOtmm(const std::string &fileName)
Definition: minimap.cpp:369
MinimapBlock::justSaw
void justSaw()
Definition: minimap.h:67
TPoint< int >
MinimapBlock::getTiles
std::array< MinimapTile, MMBLOCK_SIZE *MMBLOCK_SIZE > & getTiles()
Definition: minimap.h:65
Minimap::draw
void draw(const Rect &screenRect, const Position &mapCenter, float scale, const Color &color)
Definition: minimap.cpp:101
uint8
uint8_t uint8
Definition: types.h:37
Minimap::loadImage
bool loadImage(const std::string &fileName, const Position &topLeft, float colorFactor)
Definition: minimap.cpp:225
MinimapBlock::wasSeen
bool wasSeen()
Definition: minimap.h:68