unordered map on map
This commit is contained in:
parent
0a268fc7d9
commit
0af7856475
|
@ -24,6 +24,7 @@
|
|||
#include <type_traits>
|
||||
#include <functional>
|
||||
#include <regex>
|
||||
#include <unordered_map>
|
||||
|
||||
// string algorithms
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
class Game
|
||||
{
|
||||
public:
|
||||
|
||||
void setProtocol(ProtocolGame *protocolGame) { m_protocolGame = protocolGame; }
|
||||
ProtocolGame *getProtocol() { return m_protocolGame; }
|
||||
|
||||
|
|
36
src/map.cpp
36
src/map.cpp
|
@ -9,30 +9,34 @@ void Map::draw(int x, int y)
|
|||
|
||||
m_framebuffer->bind();
|
||||
|
||||
Position playerPos = g_game.getPlayer()->getPosition();
|
||||
Position *playerPos = g_game.getPlayer()->getPosition();
|
||||
|
||||
// player is above 7
|
||||
if(playerPos.getZ() <= 7) {
|
||||
if(playerPos->z <= 7) {
|
||||
|
||||
// player pos it 8-6. check if we can draw upper floors.
|
||||
bool draw = true;
|
||||
/*bool draw = true;
|
||||
for(int jz = 6; jz >= 0; --jz) {
|
||||
if(m_map[8+(6-jz)][6+(6-jz)][jz].getStackSize() > 0) {
|
||||
if(m_tiles[Position(8+(6-jz), 6+(6-jz), jz)]->getStackSize() > 0) {
|
||||
draw = false;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
for(int iz = 7; iz > 0; --iz) {
|
||||
|
||||
// +1 in draws cause 64x64 items may affect view.
|
||||
for(int ix = 0; ix < 24; ++ix) {
|
||||
for(int iy = 0; iy < 20; ++iy) {
|
||||
m_map[ix+1][iy+1][iz].draw(ix, iy, iz);
|
||||
for(int ix = - 8; ix < + 8; ++ix) {
|
||||
for(int iy = - 6; iy < + 6; ++iy) {
|
||||
Position relativePos = Position(playerPos->x + ix, playerPos->y + iy, iz);
|
||||
//Position drawPos = Position(ix + 8, iy - playerPos->y + 6, iz);
|
||||
//logDebug("x: ", relativePos.x, " y: ", relativePos.y, " z: ", (int)relativePos.z);
|
||||
if(m_tiles.find(relativePos) != m_tiles.end())
|
||||
m_tiles[relativePos]->draw(ix + 8, iy + 6, iz);
|
||||
}
|
||||
}
|
||||
|
||||
if(!draw)
|
||||
break;
|
||||
//if(!draw)
|
||||
//break;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -44,14 +48,8 @@ void Map::draw(int x, int y)
|
|||
|
||||
void Map::addThing(Thing *thing, const Position& pos)
|
||||
{
|
||||
Position playerPos = g_game.getPlayer()->getPosition();
|
||||
Position relativePos = Position(pos.getX() - playerPos.getX() + 8, pos.getY() - playerPos.getY() + 6, pos.getZ());
|
||||
|
||||
if(relativePos.getX() >= 25 || relativePos.getY() >= 21 || relativePos.getZ() >= 15) {
|
||||
logDebug("relativePos is invalid.");
|
||||
return;
|
||||
if(m_tiles.find(pos) == m_tiles.end()) {
|
||||
m_tiles[pos] = TilePtr(new Tile());
|
||||
}
|
||||
|
||||
//logDebug("x: ", (int)relativePos.getX(), " y: ", (int)relativePos.getY(), " z: ", (int)relativePos.getZ());
|
||||
m_map[relativePos.getX()][relativePos.getY()][relativePos.getZ()].addThing(thing);
|
||||
m_tiles[pos]->addThing(thing);
|
||||
}
|
||||
|
|
|
@ -13,8 +13,7 @@ public:
|
|||
void draw(int x, int y);
|
||||
|
||||
private:
|
||||
// Visible tiles are 15x11, but we have a +7 value cause itens visible at other floors. We have 15 floors.
|
||||
Tile m_map[25][21][15];
|
||||
std::unordered_map<Position, TilePtr, PositionHash> m_tiles;
|
||||
|
||||
FrameBufferPtr m_framebuffer;
|
||||
};
|
||||
|
|
|
@ -8,7 +8,7 @@ class Player
|
|||
public:
|
||||
|
||||
void setPosition(const Position& position) { m_position = position; }
|
||||
Position getPosition() { return m_position; }
|
||||
Position *getPosition() { return &m_position; }
|
||||
|
||||
private:
|
||||
Position m_position;
|
||||
|
|
|
@ -7,25 +7,24 @@ class Position
|
|||
{
|
||||
public:
|
||||
Position(uint16 x = 0, uint16 y = 0, uint8 z = 0) {
|
||||
m_x = x;
|
||||
m_y = y;
|
||||
m_z = z;
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
this->z = z;
|
||||
}
|
||||
|
||||
void setX(uint16 x) { m_x = x; }
|
||||
void setY(uint16 y) { m_y = y; }
|
||||
void setZ(uint8 z) { m_z = z; }
|
||||
bool operator==(const Position& other) const { return other.x == x && other.y == y && other.z == z; }
|
||||
const Position operator+(const Position& other) const { return Position(other.x + x, other.y + y, other.z + z); }
|
||||
const Position operator-(const Position& other) const { return Position(other.x - x, other.y - y, other.z - z); }
|
||||
|
||||
uint16 getX() const { return m_x; }
|
||||
uint16 getY() const { return m_y; }
|
||||
uint8 getZ() const { return m_z; }
|
||||
uint16 x, y;
|
||||
uint8 z;
|
||||
};
|
||||
|
||||
//const Position operator+(const Position& other) const { return Position(other.m_x + m_x, other.m_y + m_y, other.m_z + m_z); }
|
||||
//const Position operator-(const Position& other) const { return Position(other.m_x - m_x, other.m_y - m_y, other.m_z - m_z); }
|
||||
|
||||
private:
|
||||
uint16 m_x, m_y;
|
||||
uint8 m_z;
|
||||
struct PositionHash : public std::unary_function<Position, size_t>
|
||||
{
|
||||
size_t operator()(const Position& pos) const {
|
||||
return ((((pos.x * 65536) + pos.y) * 15) + pos.z) % (1 * 1000000);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -280,36 +280,41 @@ void ProtocolGame::parseCanReportBugs(InputMessage& msg)
|
|||
void ProtocolGame::parseMapDescription(InputMessage& msg)
|
||||
{
|
||||
Player *player = g_game.getPlayer();
|
||||
player->setPosition(parsePosition(msg));
|
||||
setMapDescription(msg, player->getPosition().getX() - 8, player->getPosition().getY() - 6, player->getPosition().getZ(), 18, 14);
|
||||
|
||||
Position playerPos = parsePosition(msg);
|
||||
|
||||
logDebug("x: ", playerPos.x, " y: ", playerPos.y, " z: ", (int)playerPos.z);
|
||||
player->setPosition(playerPos);
|
||||
logDebug("x: ", player->getPosition()->x, " y: ", player->getPosition()->y, " z: ", (int)player->getPosition()->z);
|
||||
setMapDescription(msg, player->getPosition()->x - 8, player->getPosition()->y - 6, player->getPosition()->z, 18, 14);
|
||||
}
|
||||
|
||||
void ProtocolGame::parseMoveNorth(InputMessage& msg)
|
||||
{
|
||||
Player *player = g_game.getPlayer();
|
||||
player->getPosition().setY(player->getPosition().getY() - 1);
|
||||
setMapDescription(msg, player->getPosition().getX() - 8, player->getPosition().getY() - 6, player->getPosition().getZ(), 18, 1);
|
||||
player->getPosition()->y--;
|
||||
setMapDescription(msg, player->getPosition()->x - 8, player->getPosition()->y - 6, player->getPosition()->z, 18, 1);
|
||||
}
|
||||
|
||||
void ProtocolGame::parseMoveEast(InputMessage& msg)
|
||||
{
|
||||
Player *player = g_game.getPlayer();
|
||||
player->getPosition().setX(player->getPosition().getX() + 1);
|
||||
setMapDescription(msg, player->getPosition().getX() + 9, player->getPosition().getY() - 6, player->getPosition().getZ(), 1, 14);
|
||||
player->getPosition()->x++;
|
||||
setMapDescription(msg, player->getPosition()->x + 9, player->getPosition()->y - 6, player->getPosition()->z, 1, 14);
|
||||
}
|
||||
|
||||
void ProtocolGame::parseMoveSouth(InputMessage& msg)
|
||||
{
|
||||
Player *player = g_game.getPlayer();
|
||||
player->getPosition().setY(player->getPosition().getY() + 1);
|
||||
setMapDescription(msg, player->getPosition().getX() - 8, player->getPosition().getY() + 7, player->getPosition().getZ(), 18, 1);
|
||||
player->getPosition()->y++;
|
||||
setMapDescription(msg, player->getPosition()->x - 8, player->getPosition()->y + 7, player->getPosition()->z, 18, 1);
|
||||
}
|
||||
|
||||
void ProtocolGame::parseMoveWest(InputMessage& msg)
|
||||
{
|
||||
Player *player = g_game.getPlayer();
|
||||
player->getPosition().setX(player->getPosition().getX() - 1);
|
||||
setMapDescription(msg, player->getPosition().getX() - 8, player->getPosition().getY() - 6, player->getPosition().getZ(), 1, 14);
|
||||
player->getPosition()->x--;
|
||||
setMapDescription(msg, player->getPosition()->x - 8, player->getPosition()->y - 6, player->getPosition()->z, 1, 14);
|
||||
}
|
||||
|
||||
void ProtocolGame::parseUpdateTile(InputMessage& msg)
|
||||
|
@ -685,35 +690,35 @@ void ProtocolGame::parseCancelWalk(InputMessage& msg)
|
|||
void ProtocolGame::parseFloorChangeUp(InputMessage& msg)
|
||||
{
|
||||
Player *player = g_game.getPlayer();
|
||||
player->getPosition().setZ(player->getPosition().getZ() - 1);
|
||||
player->getPosition()->z--;
|
||||
|
||||
int32 skip = 0;
|
||||
if(player->getPosition().getZ() == 7)
|
||||
if(player->getPosition()->z == 7)
|
||||
for(int32 i = 5; i >= 0; i--)
|
||||
setFloorDescription(msg, player->getPosition().getX() - 8, player->getPosition().getY() - 6, i, 18, 14, 8 - i, &skip);
|
||||
else if(player->getPosition().getZ() > 7)
|
||||
setFloorDescription(msg, player->getPosition().getX() - 8, player->getPosition().getY() - 6, player->getPosition().getZ() - 2, 18, 14, 3, &skip);
|
||||
setFloorDescription(msg, player->getPosition()->x - 8, player->getPosition()->y - 6, i, 18, 14, 8 - i, &skip);
|
||||
else if(player->getPosition()->z > 7)
|
||||
setFloorDescription(msg, player->getPosition()->x - 8, player->getPosition()->y - 6, player->getPosition()->z - 2, 18, 14, 3, &skip);
|
||||
|
||||
player->getPosition().setX(player->getPosition().getX() + 1);
|
||||
player->getPosition().setY(player->getPosition().getY() + 1);
|
||||
player->getPosition()->x++;
|
||||
player->getPosition()->y++;
|
||||
}
|
||||
|
||||
void ProtocolGame::parseFloorChangeDown(InputMessage& msg)
|
||||
{
|
||||
Player *player = g_game.getPlayer();
|
||||
player->getPosition().setZ(player->getPosition().getZ() + 1);
|
||||
player->getPosition()->z++;
|
||||
|
||||
int32 skip = 0;
|
||||
if(player->getPosition().getZ() == 8) {
|
||||
if(player->getPosition()->z == 8) {
|
||||
int32 j, i;
|
||||
for(i = player->getPosition().getZ(), j = -1; i < (int32)player->getPosition().getZ() + 3; ++i, --j)
|
||||
setFloorDescription(msg, player->getPosition().getX() - 8, player->getPosition().getY() - 6, i, 18, 14, j, &skip);
|
||||
for(i = player->getPosition()->z, j = -1; i < (int32)player->getPosition()->z + 3; ++i, --j)
|
||||
setFloorDescription(msg, player->getPosition()->x - 8, player->getPosition()->y - 6, i, 18, 14, j, &skip);
|
||||
}
|
||||
else if(player->getPosition().getZ() > 8 && player->getPosition().getZ() < 14)
|
||||
setFloorDescription(msg, player->getPosition().getX() - 8, player->getPosition().getY() - 6, player->getPosition().getZ() + 2, 18, 14, -3, &skip);
|
||||
else if(player->getPosition()->z > 8 && player->getPosition()->z < 14)
|
||||
setFloorDescription(msg, player->getPosition()->x - 8, player->getPosition()->y - 6, player->getPosition()->z + 2, 18, 14, -3, &skip);
|
||||
|
||||
player->getPosition().setX(player->getPosition().getX() - 1);
|
||||
player->getPosition().setY(player->getPosition().getY() - 1);
|
||||
player->getPosition()->x--;
|
||||
player->getPosition()->y--;
|
||||
}
|
||||
|
||||
void ProtocolGame::parseOutfitWindow(InputMessage& msg)
|
||||
|
@ -792,6 +797,8 @@ void ProtocolGame::setMapDescription(InputMessage& msg, int32 x, int32 y, int32
|
|||
zstep = -1;
|
||||
}
|
||||
|
||||
logDebug((int)startz);
|
||||
|
||||
for(int nz = startz; nz != endz + zstep; nz += zstep)
|
||||
setFloorDescription(msg, x, y, nz, width, height, z - nz, &skip);
|
||||
}
|
||||
|
|
|
@ -4,6 +4,9 @@
|
|||
#include <global.h>
|
||||
#include "thing.h"
|
||||
|
||||
class Tile;
|
||||
typedef std::shared_ptr<Tile> TilePtr;
|
||||
|
||||
class Tile
|
||||
{
|
||||
public:
|
||||
|
|
Loading…
Reference in New Issue