unordered map on map

This commit is contained in:
Henrique 2011-08-13 21:30:27 -03:00
parent 0a268fc7d9
commit 0af7856475
8 changed files with 70 additions and 62 deletions

View File

@ -24,6 +24,7 @@
#include <type_traits> #include <type_traits>
#include <functional> #include <functional>
#include <regex> #include <regex>
#include <unordered_map>
// string algorithms // string algorithms
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>

View File

@ -9,6 +9,7 @@
class Game class Game
{ {
public: public:
void setProtocol(ProtocolGame *protocolGame) { m_protocolGame = protocolGame; } void setProtocol(ProtocolGame *protocolGame) { m_protocolGame = protocolGame; }
ProtocolGame *getProtocol() { return m_protocolGame; } ProtocolGame *getProtocol() { return m_protocolGame; }

View File

@ -9,30 +9,34 @@ void Map::draw(int x, int y)
m_framebuffer->bind(); m_framebuffer->bind();
Position playerPos = g_game.getPlayer()->getPosition(); Position *playerPos = g_game.getPlayer()->getPosition();
// player is above 7 // player is above 7
if(playerPos.getZ() <= 7) { if(playerPos->z <= 7) {
// player pos it 8-6. check if we can draw upper floors. // 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) { 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; draw = false;
} }
} }*/
for(int iz = 7; iz > 0; --iz) { for(int iz = 7; iz > 0; --iz) {
// +1 in draws cause 64x64 items may affect view. // +1 in draws cause 64x64 items may affect view.
for(int ix = 0; ix < 24; ++ix) { for(int ix = - 8; ix < + 8; ++ix) {
for(int iy = 0; iy < 20; ++iy) { for(int iy = - 6; iy < + 6; ++iy) {
m_map[ix+1][iy+1][iz].draw(ix, iy, iz); 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) //if(!draw)
break; //break;
} }
} }
@ -44,14 +48,8 @@ void Map::draw(int x, int y)
void Map::addThing(Thing *thing, const Position& pos) void Map::addThing(Thing *thing, const Position& pos)
{ {
Position playerPos = g_game.getPlayer()->getPosition(); if(m_tiles.find(pos) == m_tiles.end()) {
Position relativePos = Position(pos.getX() - playerPos.getX() + 8, pos.getY() - playerPos.getY() + 6, pos.getZ()); m_tiles[pos] = TilePtr(new Tile());
if(relativePos.getX() >= 25 || relativePos.getY() >= 21 || relativePos.getZ() >= 15) {
logDebug("relativePos is invalid.");
return;
} }
m_tiles[pos]->addThing(thing);
//logDebug("x: ", (int)relativePos.getX(), " y: ", (int)relativePos.getY(), " z: ", (int)relativePos.getZ());
m_map[relativePos.getX()][relativePos.getY()][relativePos.getZ()].addThing(thing);
} }

View File

@ -13,8 +13,7 @@ public:
void draw(int x, int y); void draw(int x, int y);
private: private:
// Visible tiles are 15x11, but we have a +7 value cause itens visible at other floors. We have 15 floors. std::unordered_map<Position, TilePtr, PositionHash> m_tiles;
Tile m_map[25][21][15];
FrameBufferPtr m_framebuffer; FrameBufferPtr m_framebuffer;
}; };

View File

@ -8,7 +8,7 @@ class Player
public: public:
void setPosition(const Position& position) { m_position = position; } void setPosition(const Position& position) { m_position = position; }
Position getPosition() { return m_position; } Position *getPosition() { return &m_position; }
private: private:
Position m_position; Position m_position;

View File

@ -7,25 +7,24 @@ class Position
{ {
public: public:
Position(uint16 x = 0, uint16 y = 0, uint8 z = 0) { Position(uint16 x = 0, uint16 y = 0, uint8 z = 0) {
m_x = x; this->x = x;
m_y = y; this->y = y;
m_z = z; this->z = z;
} }
void setX(uint16 x) { m_x = x; } bool operator==(const Position& other) const { return other.x == x && other.y == y && other.z == z; }
void setY(uint16 y) { m_y = y; } const Position operator+(const Position& other) const { return Position(other.x + x, other.y + y, other.z + z); }
void setZ(uint8 z) { m_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 x, y;
uint16 getY() const { return m_y; } uint8 z;
uint8 getZ() const { return 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); } struct PositionHash : public std::unary_function<Position, size_t>
//const Position operator-(const Position& other) const { return Position(other.m_x - m_x, other.m_y - m_y, other.m_z - m_z); } {
size_t operator()(const Position& pos) const {
private: return ((((pos.x * 65536) + pos.y) * 15) + pos.z) % (1 * 1000000);
uint16 m_x, m_y; }
uint8 m_z;
}; };
#endif #endif

View File

@ -280,36 +280,41 @@ void ProtocolGame::parseCanReportBugs(InputMessage& msg)
void ProtocolGame::parseMapDescription(InputMessage& msg) void ProtocolGame::parseMapDescription(InputMessage& msg)
{ {
Player *player = g_game.getPlayer(); 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) void ProtocolGame::parseMoveNorth(InputMessage& msg)
{ {
Player *player = g_game.getPlayer(); Player *player = g_game.getPlayer();
player->getPosition().setY(player->getPosition().getY() - 1); player->getPosition()->y--;
setMapDescription(msg, player->getPosition().getX() - 8, player->getPosition().getY() - 6, player->getPosition().getZ(), 18, 1); setMapDescription(msg, player->getPosition()->x - 8, player->getPosition()->y - 6, player->getPosition()->z, 18, 1);
} }
void ProtocolGame::parseMoveEast(InputMessage& msg) void ProtocolGame::parseMoveEast(InputMessage& msg)
{ {
Player *player = g_game.getPlayer(); Player *player = g_game.getPlayer();
player->getPosition().setX(player->getPosition().getX() + 1); player->getPosition()->x++;
setMapDescription(msg, player->getPosition().getX() + 9, player->getPosition().getY() - 6, player->getPosition().getZ(), 1, 14); setMapDescription(msg, player->getPosition()->x + 9, player->getPosition()->y - 6, player->getPosition()->z, 1, 14);
} }
void ProtocolGame::parseMoveSouth(InputMessage& msg) void ProtocolGame::parseMoveSouth(InputMessage& msg)
{ {
Player *player = g_game.getPlayer(); Player *player = g_game.getPlayer();
player->getPosition().setY(player->getPosition().getY() + 1); player->getPosition()->y++;
setMapDescription(msg, player->getPosition().getX() - 8, player->getPosition().getY() + 7, player->getPosition().getZ(), 18, 1); setMapDescription(msg, player->getPosition()->x - 8, player->getPosition()->y + 7, player->getPosition()->z, 18, 1);
} }
void ProtocolGame::parseMoveWest(InputMessage& msg) void ProtocolGame::parseMoveWest(InputMessage& msg)
{ {
Player *player = g_game.getPlayer(); Player *player = g_game.getPlayer();
player->getPosition().setX(player->getPosition().getX() - 1); player->getPosition()->x--;
setMapDescription(msg, player->getPosition().getX() - 8, player->getPosition().getY() - 6, player->getPosition().getZ(), 1, 14); setMapDescription(msg, player->getPosition()->x - 8, player->getPosition()->y - 6, player->getPosition()->z, 1, 14);
} }
void ProtocolGame::parseUpdateTile(InputMessage& msg) void ProtocolGame::parseUpdateTile(InputMessage& msg)
@ -685,35 +690,35 @@ void ProtocolGame::parseCancelWalk(InputMessage& msg)
void ProtocolGame::parseFloorChangeUp(InputMessage& msg) void ProtocolGame::parseFloorChangeUp(InputMessage& msg)
{ {
Player *player = g_game.getPlayer(); Player *player = g_game.getPlayer();
player->getPosition().setZ(player->getPosition().getZ() - 1); player->getPosition()->z--;
int32 skip = 0; int32 skip = 0;
if(player->getPosition().getZ() == 7) if(player->getPosition()->z == 7)
for(int32 i = 5; i >= 0; i--) for(int32 i = 5; i >= 0; i--)
setFloorDescription(msg, player->getPosition().getX() - 8, player->getPosition().getY() - 6, i, 18, 14, 8 - i, &skip); setFloorDescription(msg, player->getPosition()->x - 8, player->getPosition()->y - 6, i, 18, 14, 8 - i, &skip);
else if(player->getPosition().getZ() > 7) else if(player->getPosition()->z > 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, player->getPosition()->z - 2, 18, 14, 3, &skip);
player->getPosition().setX(player->getPosition().getX() + 1); player->getPosition()->x++;
player->getPosition().setY(player->getPosition().getY() + 1); player->getPosition()->y++;
} }
void ProtocolGame::parseFloorChangeDown(InputMessage& msg) void ProtocolGame::parseFloorChangeDown(InputMessage& msg)
{ {
Player *player = g_game.getPlayer(); Player *player = g_game.getPlayer();
player->getPosition().setZ(player->getPosition().getZ() + 1); player->getPosition()->z++;
int32 skip = 0; int32 skip = 0;
if(player->getPosition().getZ() == 8) { if(player->getPosition()->z == 8) {
int32 j, i; int32 j, i;
for(i = player->getPosition().getZ(), j = -1; i < (int32)player->getPosition().getZ() + 3; ++i, --j) for(i = player->getPosition()->z, j = -1; i < (int32)player->getPosition()->z + 3; ++i, --j)
setFloorDescription(msg, player->getPosition().getX() - 8, player->getPosition().getY() - 6, i, 18, 14, j, &skip); 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) else if(player->getPosition()->z > 8 && player->getPosition()->z < 14)
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, player->getPosition()->z + 2, 18, 14, -3, &skip);
player->getPosition().setX(player->getPosition().getX() - 1); player->getPosition()->x--;
player->getPosition().setY(player->getPosition().getY() - 1); player->getPosition()->y--;
} }
void ProtocolGame::parseOutfitWindow(InputMessage& msg) void ProtocolGame::parseOutfitWindow(InputMessage& msg)
@ -792,6 +797,8 @@ void ProtocolGame::setMapDescription(InputMessage& msg, int32 x, int32 y, int32
zstep = -1; zstep = -1;
} }
logDebug((int)startz);
for(int nz = startz; nz != endz + zstep; nz += zstep) for(int nz = startz; nz != endz + zstep; nz += zstep)
setFloorDescription(msg, x, y, nz, width, height, z - nz, &skip); setFloorDescription(msg, x, y, nz, width, height, z - nz, &skip);
} }

View File

@ -4,6 +4,9 @@
#include <global.h> #include <global.h>
#include "thing.h" #include "thing.h"
class Tile;
typedef std::shared_ptr<Tile> TilePtr;
class Tile class Tile
{ {
public: public: