tibia-client/src/client/minimap.h

115 lines
4.6 KiB
C
Raw Normal View History

2012-06-26 00:13:30 +02:00
/*
2014-04-01 07:36:42 +02:00
* Copyright (c) 2010-2014 OTClient <https://github.com/edubart/otclient>
2012-06-26 00:13:30 +02:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MINIMAP_H
#define MINIMAP_H
2013-01-21 00:17:56 +01:00
#include "declarations.h"
#include <framework/graphics/declarations.h>
2012-06-26 00:13:30 +02:00
enum {
MMBLOCK_SIZE = 64,
OTMM_SIGNATURE = 0x4D4d544F,
OTMM_VERSION = 1
2013-01-21 00:17:56 +01:00
};
enum MinimapTileFlags {
MinimapTileWasSeen = 1,
MinimapTileNotPathable = 2,
MinimapTileNotWalkable = 4
2012-06-26 00:13:30 +02:00
};
2013-01-21 00:17:56 +01:00
#pragma pack(push,1) // disable memory alignment
struct MinimapTile
2012-06-26 00:13:30 +02:00
{
MinimapTile() : flags(0), color(255), speed(10) { }
2013-01-21 00:17:56 +01:00
uint8 flags;
uint8 color;
uint8 speed;
bool hasFlag(MinimapTileFlags flag) const { return flags & flag; }
int getSpeed() const { return speed * 10; }
bool operator==(const MinimapTile& other) const { return color == other.color && flags == other.flags && speed == other.speed; }
bool operator!=(const MinimapTile& other) const { return !(*this == other); }
2013-01-21 00:17:56 +01:00
};
class MinimapBlock
{
public:
void clean();
void update();
void updateTile(int x, int y, const MinimapTile& tile);
MinimapTile& getTile(int x, int y) { return m_tiles[getTileIndex(x,y)]; }
void resetTile(int x, int y) { m_tiles[getTileIndex(x,y)] = MinimapTile(); }
uint getTileIndex(int x, int y) { return ((y % MMBLOCK_SIZE) * MMBLOCK_SIZE) + (x % MMBLOCK_SIZE); }
const TexturePtr& getTexture() { return m_texture; }
std::array<MinimapTile, MMBLOCK_SIZE *MMBLOCK_SIZE>& getTiles() { return m_tiles; }
void mustUpdate() { m_mustUpdate = true; }
void justSaw() { m_wasSeen = true; }
bool wasSeen() { return m_wasSeen; }
2013-01-21 00:17:56 +01:00
private:
TexturePtr m_texture;
std::array<MinimapTile, MMBLOCK_SIZE *MMBLOCK_SIZE> m_tiles;
stdext::boolean<true> m_mustUpdate;
stdext::boolean<false> m_wasSeen;
2012-06-26 00:13:30 +02:00
};
#pragma pack(pop)
2012-06-26 00:13:30 +02:00
class Minimap
{
public:
void init();
void terminate();
2013-01-21 00:17:56 +01:00
void clean();
2012-06-26 00:13:30 +02:00
void draw(const Rect& screenRect, const Position& mapCenter, float scale, const Color& color);
Point getTilePoint(const Position& pos, const Rect& screenRect, const Position& mapCenter, float scale);
Position getTilePosition(const Point& point, const Rect& screenRect, const Position& mapCenter, float scale);
Rect getTileRect(const Position& pos, const Rect& screenRect, const Position& mapCenter, float scale);
2012-06-26 00:13:30 +02:00
2013-01-21 00:17:56 +01:00
void updateTile(const Position& pos, const TilePtr& tile);
const MinimapTile& getTile(const Position& pos);
2012-06-26 00:13:30 +02:00
bool loadImage(const std::string& fileName, const Position& topLeft, float colorFactor);
void saveImage(const std::string& fileName, const Rect& mapRect);
bool loadOtmm(const std::string& fileName);
2013-01-21 00:17:56 +01:00
void saveOtmm(const std::string& fileName);
private:
2013-01-31 06:38:06 +01:00
Rect calcMapRect(const Rect& screenRect, const Position& mapCenter, float scale);
bool hasBlock(const Position& pos) { return m_tileBlocks[pos.z].find(getBlockIndex(pos)) != m_tileBlocks[pos.z].end(); }
2013-01-21 00:17:56 +01:00
MinimapBlock& getBlock(const Position& pos) { return m_tileBlocks[pos.z][getBlockIndex(pos)]; }
Point getBlockOffset(const Point& pos) { return Point(pos.x - pos.x % MMBLOCK_SIZE,
pos.y - pos.y % MMBLOCK_SIZE); }
Position getIndexPosition(int index, int z) { return Position((index % (65536 / MMBLOCK_SIZE))*MMBLOCK_SIZE,
(index / (65536 / MMBLOCK_SIZE))*MMBLOCK_SIZE, z); }
2013-01-21 00:17:56 +01:00
uint getBlockIndex(const Position& pos) { return ((pos.y / MMBLOCK_SIZE) * (65536 / MMBLOCK_SIZE)) + (pos.x / MMBLOCK_SIZE); }
std::unordered_map<uint, MinimapBlock> m_tileBlocks[Otc::MAX_Z+1];
2012-06-26 00:13:30 +02:00
};
extern Minimap g_minimap;
2013-01-21 00:17:56 +01:00
2012-06-26 00:13:30 +02:00
#endif