[General] Implement sending of image data for map tiles in PlayerMap

0.6.3
David Cernat 7 years ago
parent 980ddcb114
commit 715012f087

@ -17,7 +17,7 @@ void CellFunctions::InitializeMapChanges(unsigned short pid) noexcept
Player *player; Player *player;
GET_PLAYER(pid, player, ); GET_PLAYER(pid, player, );
player->mapChanges.cellsExplored.clear(); player->mapChanges.mapTiles.clear();
} }
unsigned int CellFunctions::GetCellStateChangesSize(unsigned short pid) noexcept unsigned int CellFunctions::GetCellStateChangesSize(unsigned short pid) noexcept
@ -28,6 +28,14 @@ unsigned int CellFunctions::GetCellStateChangesSize(unsigned short pid) noexcept
return player->cellStateChanges.count; return player->cellStateChanges.count;
} }
unsigned int CellFunctions::GetMapChangesSize(unsigned short pid) noexcept
{
Player *player;
GET_PLAYER(pid, player, 0);
return player->mapChanges.mapTiles.size();
}
unsigned int CellFunctions::GetCellStateType(unsigned short pid, unsigned int i) noexcept unsigned int CellFunctions::GetCellStateType(unsigned short pid, unsigned int i) noexcept
{ {
Player *player; Player *player;
@ -95,6 +103,35 @@ bool CellFunctions::IsChangingRegion(unsigned short pid) noexcept
return player->isChangingRegion; return player->isChangingRegion;
} }
void CellFunctions::SaveMapTileImageFile(unsigned short pid, unsigned int i, const char *filePath) noexcept
{
Player *player;
GET_PLAYER(pid, player,);
if (i >= player->mapChanges.mapTiles.size())
return;
const std::vector<char>& imageData = player->mapChanges.mapTiles.at(i).imageData;
std::ofstream output_file(filePath, std::ios::binary);
std::ostream_iterator<char> output_iterator(output_file);
std::copy(imageData.begin(), imageData.end(), output_iterator);
}
int CellFunctions::GetMapTileCellX(unsigned short pid, unsigned int i) noexcept
{
Player *player;
GET_PLAYER(pid, player, 0);
return player->mapChanges.mapTiles.at(i).x;
}
int CellFunctions::GetMapTileCellY(unsigned short pid, unsigned int i) noexcept
{
Player *player;
GET_PLAYER(pid, player, 0);
return player->mapChanges.mapTiles.at(i).y;
}
void CellFunctions::SetCell(unsigned short pid, const char *cellDescription) noexcept void CellFunctions::SetCell(unsigned short pid, const char *cellDescription) noexcept
{ {
Player *player; Player *player;
@ -123,13 +160,19 @@ void CellFunctions::SetExteriorCell(unsigned short pid, int x, int y) noexcept
player->cell.mData.mY = y; player->cell.mData.mY = y;
} }
void CellFunctions::AddCellExplored(unsigned short pid, const char* cellDescription) noexcept void CellFunctions::LoadMapTileImageFile(unsigned short pid, int cellX, int cellY, const char* filePath) noexcept
{ {
Player *player; Player *player;
GET_PLAYER(pid, player, ); GET_PLAYER(pid, player, );
ESM::Cell cellExplored = Utils::getCellFromDescription(cellDescription); mwmp::MapTile mapTile;
player->mapChanges.cellsExplored.push_back(cellExplored); mapTile.x = cellX;
mapTile.y = cellY;
std::ifstream fin(filePath, std::ios::binary);
mapTile.imageData = std::vector<char>(std::istreambuf_iterator<char>(fin), std::istreambuf_iterator<char>());
player->mapChanges.mapTiles.push_back(mapTile);
} }
void CellFunctions::SendCell(unsigned short pid) noexcept void CellFunctions::SendCell(unsigned short pid) noexcept

@ -7,6 +7,7 @@
{"InitializeMapChanges", CellFunctions::InitializeMapChanges},\ {"InitializeMapChanges", CellFunctions::InitializeMapChanges},\
\ \
{"GetCellStateChangesSize", CellFunctions::GetCellStateChangesSize},\ {"GetCellStateChangesSize", CellFunctions::GetCellStateChangesSize},\
{"GetMapChangesSize", CellFunctions::GetMapChangesSize},\
\ \
{"GetCellStateType", CellFunctions::GetCellStateType},\ {"GetCellStateType", CellFunctions::GetCellStateType},\
{"GetCellStateDescription", CellFunctions::GetCellStateDescription},\ {"GetCellStateDescription", CellFunctions::GetCellStateDescription},\
@ -19,10 +20,14 @@
{"GetRegion", CellFunctions::GetRegion},\ {"GetRegion", CellFunctions::GetRegion},\
{"IsChangingRegion", CellFunctions::IsChangingRegion},\ {"IsChangingRegion", CellFunctions::IsChangingRegion},\
\ \
{"GetMapTileCellX", CellFunctions::GetMapTileCellX},\
{"GetMapTileCellY", CellFunctions::GetMapTileCellY},\
{"SaveMapTileImageFile", CellFunctions::SaveMapTileImageFile},\
\
{"SetCell", CellFunctions::SetCell},\ {"SetCell", CellFunctions::SetCell},\
{"SetExteriorCell", CellFunctions::SetExteriorCell},\ {"SetExteriorCell", CellFunctions::SetExteriorCell},\
\ \
{"AddCellExplored", CellFunctions::AddCellExplored},\ {"LoadMapTileImageFile", CellFunctions::LoadMapTileImageFile},\
\ \
{"SendCell", CellFunctions::SendCell},\ {"SendCell", CellFunctions::SendCell},\
{"SendMapChanges", CellFunctions::SendMapChanges} {"SendMapChanges", CellFunctions::SendMapChanges}
@ -50,6 +55,14 @@ public:
*/ */
static unsigned int GetCellStateChangesSize(unsigned short pid) noexcept; static unsigned int GetCellStateChangesSize(unsigned short pid) noexcept;
/**
* \brief Get the number of indexes in a player's latest map changes.
*
* \param pid The player ID whose map changes should be used.
* \return The number of indexes.
*/
static unsigned int GetMapChangesSize(unsigned short pid) noexcept;
/** /**
* \brief Get the cell state type at a certain index in a player's latest cell state changes. * \brief Get the cell state type at a certain index in a player's latest cell state changes.
* *
@ -118,6 +131,37 @@ public:
*/ */
static bool IsChangingRegion(unsigned short pid) noexcept; static bool IsChangingRegion(unsigned short pid) noexcept;
/**
* \brief Get the X coordinate of the cell corresponding to the map tile at a certain index in a
* player's latest map changes.
*
* \param pid The player ID whose map changes should be used.
* \param i The index of the map tile.
* \return The X coordinate of the cell.
*/
static int GetMapTileCellX(unsigned short pid, unsigned int i) noexcept;
/**
* \brief Get the Y coordinate of the cell corresponding to the map tile at a certain index in a
* player's latest map changes.
*
* \param pid The player ID whose map changes should be used.
* \param i The index of the map tile.
* \return The Y coordinate of the cell.
*/
static int GetMapTileCellY(unsigned short pid, unsigned int i) noexcept;
/**
* \brief Save the .png image data of the map tile at a certain index in a player's latest map changes
* to a file.
*
* \param pid The player ID whose map changes should be used.
* \param i The index of the map tile.
* \param filePath The file path of the resulting file.
* \return void
*/
static void SaveMapTileImageFile(unsigned short pid, unsigned int i, const char *filePath) noexcept;
/** /**
* \brief Set the cell of a player. * \brief Set the cell of a player.
* *
@ -147,13 +191,16 @@ public:
static void SetExteriorCell(unsigned short pid, int x, int y) noexcept; static void SetExteriorCell(unsigned short pid, int x, int y) noexcept;
/** /**
* \brief Add a new explored cell to the map changes for a player. * \brief Load a .png file as the image data for a map tile and add it to the map changes for
* a player.
* *
* \param pid The player ID whose map changes should be used. * \param pid The player ID whose map changes should be used.
* \param cellDescription The cell description of the explored cell. * \param cellX The X coordinate of the cell corresponding to the map tile.
* \param cellY The Y coordinate of the cell corresponding to the map tile.
* \param filePath The file path of the loaded file.
* \return void * \return void
*/ */
static void AddCellExplored(unsigned short pid, const char* cellDescription) noexcept; static void LoadMapTileImageFile(unsigned short pid, int cellX, int cellY, const char* filePath) noexcept;
/** /**
* \brief Send a PlayerCellChange packet about a player. * \brief Send a PlayerCellChange packet about a player.

@ -17,9 +17,7 @@ namespace mwmp
{ {
DEBUG_PRINTF(strPacketID.c_str()); DEBUG_PRINTF(strPacketID.c_str());
// Not currently implemented Script::Call<Script::CallbackIdentity("OnPlayerMap")>(player.getId());
//
// To be dealt with later to save explored areas on local maps
} }
}; };
} }

@ -213,9 +213,10 @@ namespace MWBase
/* /*
Start of tes3mp addition Start of tes3mp addition
Allow setting a cell as explored from elsewhere in the code Allow the setting of the image data for a global map tile from elsewhere
in the code
*/ */
virtual void setCellExplored(const MWWorld::CellStore* cell) = 0; virtual void setGlobalMapImage(int cellX, int cellY, const std::vector<char>& imageData) = 0;
/* /*
End of tes3mp addition End of tes3mp addition
*/ */

@ -941,6 +941,20 @@ namespace MWGui
} }
} }
/*
Start of tes3mp addition
Allow the setting of the image data for a global map tile from elsewhere
in the code
*/
void MapWindow::setGlobalMapImage(int cellX, int cellY, const std::vector<char>& imageData)
{
mGlobalMapRender->setImage(cellX, cellY, imageData);
}
/*
End of tes3mp addition
*/
void MapWindow::updateCustomMarkers() void MapWindow::updateCustomMarkers()
{ {
LocalMapBase::updateCustomMarkers(); LocalMapBase::updateCustomMarkers();

@ -266,6 +266,17 @@ namespace MWGui
void setGlobalMapPlayerPosition (float worldX, float worldY); void setGlobalMapPlayerPosition (float worldX, float worldY);
void setGlobalMapPlayerDir(const float x, const float y); void setGlobalMapPlayerDir(const float x, const float y);
/*
Start of tes3mp addition
Allow the setting of the image data for a global map tile from elsewhere
in the code
*/
void setGlobalMapImage(int cellX, int cellY, const std::vector<char>& imageData);
/*
End of tes3mp addition
*/
void ensureGlobalMapLoaded(); void ensureGlobalMapLoaded();
virtual void onOpen(); virtual void onOpen();

@ -1005,11 +1005,12 @@ namespace MWGui
/* /*
Start of tes3mp addition Start of tes3mp addition
Allow setting a cell as explored from elsewhere in the code Allow the setting of the image data for a global map tile from elsewhere
in the code
*/ */
void WindowManager::setCellExplored(const MWWorld::CellStore* cell) void WindowManager::setGlobalMapImage(int cellX, int cellY, const std::vector<char>& imageData)
{ {
mMap->cellExplored(cell->getCell()->getGridX(), cell->getCell()->getGridY()); mMap->setGlobalMapImage(cellX, cellY, imageData);
} }
/* /*
End of tes3mp addition End of tes3mp addition

@ -246,9 +246,10 @@ namespace MWGui
/* /*
Start of tes3mp addition Start of tes3mp addition
Allow setting a cell as explored from elsewhere in the code Allow the setting of the image data for a global map tile from elsewhere
in the code
*/ */
virtual void setCellExplored(const MWWorld::CellStore* cell); virtual void setGlobalMapImage(int cellX, int cellY, const std::vector<char>& imageData);
/* /*
End of tes3mp addition End of tes3mp addition
*/ */

@ -1294,12 +1294,14 @@ void LocalPlayer::setMapExplored()
MWWorld::Ptr ptrPlayer = getPlayerPtr(); MWWorld::Ptr ptrPlayer = getPlayerPtr();
MWMechanics::NpcStats &ptrNpcStats = ptrPlayer.getClass().getNpcStats(ptrPlayer); MWMechanics::NpcStats &ptrNpcStats = ptrPlayer.getClass().getNpcStats(ptrPlayer);
for (const auto &cellExplored : mapChanges.cellsExplored) for (const auto &mapTile : mapChanges.mapTiles)
{ {
MWWorld::CellStore *ptrCellStore = Main::get().getCellController()->getCellStore(cellExplored); const MWWorld::CellStore *cellStore = MWBase::Environment::get().getWorld()->getExterior(mapTile.x, mapTile.y);
if (ptrCellStore) if (!cellStore->getCell()->mName.empty())
MWBase::Environment::get().getWindowManager()->setCellExplored(ptrCellStore); MWBase::Environment::get().getWindowManager()->addVisitedLocation(cellStore->getCell()->mName, mapTile.x, mapTile.y);
MWBase::Environment::get().getWindowManager()->setGlobalMapImage(mapTile.x, mapTile.y, mapTile.imageData);
} }
} }
@ -1648,6 +1650,23 @@ void LocalPlayer::sendSelectedSpell(const std::string& newSelectedSpellId)
getNetworking()->getPlayerPacket(ID_PLAYER_MISCELLANEOUS)->Send(); getNetworking()->getPlayerPacket(ID_PLAYER_MISCELLANEOUS)->Send();
} }
void LocalPlayer::sendMapExplored(int x, int y, const std::vector<char>& imageData)
{
mapChanges.mapTiles.clear();
mwmp::MapTile mapTile;
mapTile.x = x;
mapTile.y = y;
mapTile.imageData = imageData;
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Sending ID_PLAYER_MAP with x: %i, y: %i", x, y);
mapChanges.mapTiles.push_back(mapTile);
getNetworking()->getPlayerPacket(ID_PLAYER_MAP)->setPlayer(this);
getNetworking()->getPlayerPacket(ID_PLAYER_MAP)->Send();
}
void LocalPlayer::clearCellStates() void LocalPlayer::clearCellStates()
{ {
cellStateChanges.cellStates.clear(); cellStateChanges.cellStates.clear();

@ -64,10 +64,10 @@ namespace mwmp
void setFactions(); void setFactions();
void setKills(); void setKills();
void setBooks(); void setBooks();
void setMapExplored();
void setShapeshift(); void setShapeshift();
void setMarkLocation(); void setMarkLocation();
void setSelectedSpell(); void setSelectedSpell();
void setMapExplored();
void sendClass(); void sendClass();
void sendInventory(); void sendInventory();
@ -90,6 +90,7 @@ namespace mwmp
void sendWerewolfState(bool isWerewolf); void sendWerewolfState(bool isWerewolf);
void sendMarkLocation(const ESM::Cell& newMarkCell, const ESM::Position& newMarkPosition); void sendMarkLocation(const ESM::Cell& newMarkCell, const ESM::Position& newMarkPosition);
void sendSelectedSpell(const std::string& newSelectedSpellId); void sendSelectedSpell(const std::string& newSelectedSpellId);
void sendMapExplored(int x, int y, const std::vector<char>& imageData);
void clearCellStates(); void clearCellStates();
void clearCurrentContainer(); void clearCurrentContainer();

@ -19,6 +19,18 @@
#include <components/esm/globalmap.hpp> #include <components/esm/globalmap.hpp>
/*
Start of tes3mp addition
Include additional headers for multiplayer purposes
*/
#include <components/openmw-mp/Log.hpp>
#include "../mwmp/Main.hpp"
#include "../mwmp/LocalPlayer.hpp"
/*
End of tes3mp addition
*/
#include "../mwbase/environment.hpp" #include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp" #include "../mwbase/world.hpp"
@ -96,6 +108,17 @@ namespace
namespace MWRender namespace MWRender
{ {
/*
Start of tes3mp addition
Use maps to track which global map coordinates belong to which cell coordinates
without having to significantly change other methods
*/
std::map<int, int> originToCellX;
std::map<int, int> originToCellY;
/*
End of tes3mp addition
*/
class CreateMapWorkItem : public SceneUtil::WorkItem class CreateMapWorkItem : public SceneUtil::WorkItem
{ {
@ -382,6 +405,17 @@ namespace MWRender
if (cellX > mMaxX || cellX < mMinX || cellY > mMaxY || cellY < mMinY) if (cellX > mMaxX || cellX < mMinX || cellY > mMaxY || cellY < mMinY)
return; return;
/*
Start of tes3mp addition
Track the cell coordinates corresponding to these map image coordinates
*/
originToCellX[originX] = cellX;
originToCellY[originY - mCellSize] = cellY;
/*
End of tes3mp addition
*/
requestOverlayTextureUpdate(originX, mHeight - originY, mCellSize, mCellSize, localMapTexture, false, true); requestOverlayTextureUpdate(originX, mHeight - originY, mCellSize, mCellSize, localMapTexture, false, true);
} }
@ -598,6 +632,40 @@ namespace MWRender
ensureLoaded(); ensureLoaded();
mOverlayImage->copySubImage(imageDest.mX, imageDest.mY, 0, imageDest.mImage); mOverlayImage->copySubImage(imageDest.mX, imageDest.mY, 0, imageDest.mImage);
/*
Start of tes3mp addition
Send an ID_PLAYER_MAP packet with this map tile to the server
*/
if (originToCellX.count(imageDest.mX) > 0 && originToCellY.count(imageDest.mY) > 0)
{
LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "New global map tile corresponds to cell %i, %i", originToCellX.at(imageDest.mX), originToCellY.at(imageDest.mY));
osgDB::ReaderWriter* readerwriter = osgDB::Registry::instance()->getReaderWriterForExtension("png");
if (!readerwriter)
{
std::cerr << "Error: Can't write temporary map image, no '" << "png" << "' readerwriter found" << std::endl;
return;
}
std::ostringstream ostream;
osgDB::ReaderWriter::WriteResult result = readerwriter->writeImage(*imageDest.mImage, ostream);
if (!result.success())
{
std::cerr << "Error: Can't write temporary map image: " << result.message() << " code " << result.status() << std::endl;
}
std::string stringData = ostream.str();
std::vector<char> vectorData = std::vector<char>(stringData.begin(), stringData.end());
mwmp::Main::get().getLocalPlayer()->sendMapExplored(originToCellX.at(imageDest.mX), originToCellY.at(imageDest.mY), vectorData);
}
/*
End of tes3mp addition
*/
it = mPendingImageDest.erase(it); it = mPendingImageDest.erase(it);
} }
} }
@ -607,4 +675,50 @@ namespace MWRender
cam->removeChildren(0, cam->getNumChildren()); cam->removeChildren(0, cam->getNumChildren());
mRoot->removeChild(cam); mRoot->removeChild(cam);
} }
/*
Start of tes3mp addition
Allow the setting of the image data for a global map tile from elsewhere
in the code
*/
void GlobalMap::setImage(int cellX, int cellY, const std::vector<char>& imageData)
{
Files::IMemStream istream(&imageData[0], imageData.size());
osgDB::ReaderWriter* reader = osgDB::Registry::instance()->getReaderWriterForExtension("png");
if (!reader)
{
std::cerr << "Error: Failed to read map tile image data, no png readerwriter found" << std::endl;
return;
}
osgDB::ReaderWriter::ReadResult result = reader->readImage(istream);
if (!result.success())
{
std::cerr << "Error: Can't read map tile image: " << result.message() << " code " << result.status() << std::endl;
return;
}
osg::ref_ptr<osg::Image> image = result.getImage();
int posX = (cellX - mMinX) * mCellSize;
int posY = (cellY - mMinY + 1) * mCellSize;
if (cellX > mMaxX || cellX < mMinX || cellY > mMaxY || cellY < mMinY)
return;
osg::ref_ptr<osg::Texture2D> texture(new osg::Texture2D);
texture->setImage(image);
texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
texture->setResizeNonPowerOfTwoHint(false);
requestOverlayTextureUpdate(posX, mHeight - posY, mCellSize, mCellSize, texture, true, false);
}
/*
End of tes3mp addition
*/
} }

@ -60,6 +60,17 @@ namespace MWRender
void removeCamera(osg::Camera* cam); void removeCamera(osg::Camera* cam);
/*
Start of tes3mp addition
Allow the setting of the image data for a global map tile from elsewhere
in the code
*/
void setImage(int cellX, int cellY, const std::vector<char>& imageData);
/*
End of tes3mp addition
*/
/** /**
* Mark a camera for cleanup in the next update. For internal use only. * Mark a camera for cleanup in the next update. For internal use only.
*/ */

@ -95,6 +95,13 @@ namespace mwmp
int type; // 0 - Cell load, 1 - Cell unload int type; // 0 - Cell load, 1 - Cell unload
}; };
struct MapTile
{
int x;
int y;
std::vector<char> imageData;
};
struct JournalChanges struct JournalChanges
{ {
std::vector<JournalItem> journalItems; std::vector<JournalItem> journalItems;
@ -136,8 +143,7 @@ namespace mwmp
struct MapChanges struct MapChanges
{ {
std::vector<ESM::Cell> cellsExplored; std::vector<MapTile> mapTiles;
unsigned int count;
}; };
struct InventoryChanges struct InventoryChanges

@ -13,24 +13,40 @@ void PacketPlayerMap::Packet(RakNet::BitStream *bs, bool send)
{ {
PlayerPacket::Packet(bs, send); PlayerPacket::Packet(bs, send);
uint32_t changesCount;
if (send) if (send)
player->mapChanges.count = (unsigned int)(player->mapChanges.cellsExplored.size()); changesCount = static_cast<uint32_t>(player->mapChanges.mapTiles.size());
else
player->mapChanges.cellsExplored.clear(); RW(changesCount, send);
RW(player->mapChanges.count, send); if (!send)
{
player->mapChanges.mapTiles.clear();
player->mapChanges.mapTiles.resize(changesCount);
}
for (unsigned int i = 0; i < player->mapChanges.count; i++) for (auto &&mapTile : player->mapChanges.mapTiles)
{ {
ESM::Cell cellExplored; RW(mapTile.x, send);
RW(mapTile.y, send);
uint32_t imageDataCount;
if (send) if (send)
cellExplored = player->mapChanges.cellsExplored.at(i); imageDataCount = static_cast<uint32_t>(mapTile.imageData.size());
RW(cellExplored.mData, send, 1); RW(imageDataCount, send);
RW(cellExplored.mName, send, 1);
if (!send) if (!send)
player->mapChanges.cellsExplored.push_back(cellExplored); {
mapTile.imageData.clear();
mapTile.imageData.resize(imageDataCount);
}
for (auto &&imageChar : mapTile.imageData)
{
RW(imageChar, send);
}
} }
} }

Loading…
Cancel
Save