diff --git a/apps/openmw-mp/Script/Functions/Cells.cpp b/apps/openmw-mp/Script/Functions/Cells.cpp index 90549a513..acb61013b 100644 --- a/apps/openmw-mp/Script/Functions/Cells.cpp +++ b/apps/openmw-mp/Script/Functions/Cells.cpp @@ -17,7 +17,7 @@ void CellFunctions::InitializeMapChanges(unsigned short pid) noexcept Player *player; GET_PLAYER(pid, player, ); - player->mapChanges.cellsExplored.clear(); + player->mapChanges.mapTiles.clear(); } unsigned int CellFunctions::GetCellStateChangesSize(unsigned short pid) noexcept @@ -28,6 +28,14 @@ unsigned int CellFunctions::GetCellStateChangesSize(unsigned short pid) noexcept 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 { Player *player; @@ -95,6 +103,35 @@ bool CellFunctions::IsChangingRegion(unsigned short pid) noexcept 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& imageData = player->mapChanges.mapTiles.at(i).imageData; + + std::ofstream output_file(filePath, std::ios::binary); + std::ostream_iterator 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 { Player *player; @@ -123,13 +160,19 @@ void CellFunctions::SetExteriorCell(unsigned short pid, int x, int y) noexcept 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; GET_PLAYER(pid, player, ); - ESM::Cell cellExplored = Utils::getCellFromDescription(cellDescription); - player->mapChanges.cellsExplored.push_back(cellExplored); + mwmp::MapTile mapTile; + mapTile.x = cellX; + mapTile.y = cellY; + + std::ifstream fin(filePath, std::ios::binary); + mapTile.imageData = std::vector(std::istreambuf_iterator(fin), std::istreambuf_iterator()); + + player->mapChanges.mapTiles.push_back(mapTile); } void CellFunctions::SendCell(unsigned short pid) noexcept diff --git a/apps/openmw-mp/Script/Functions/Cells.hpp b/apps/openmw-mp/Script/Functions/Cells.hpp index e027bfe52..c5731a40a 100644 --- a/apps/openmw-mp/Script/Functions/Cells.hpp +++ b/apps/openmw-mp/Script/Functions/Cells.hpp @@ -7,6 +7,7 @@ {"InitializeMapChanges", CellFunctions::InitializeMapChanges},\ \ {"GetCellStateChangesSize", CellFunctions::GetCellStateChangesSize},\ + {"GetMapChangesSize", CellFunctions::GetMapChangesSize},\ \ {"GetCellStateType", CellFunctions::GetCellStateType},\ {"GetCellStateDescription", CellFunctions::GetCellStateDescription},\ @@ -19,10 +20,14 @@ {"GetRegion", CellFunctions::GetRegion},\ {"IsChangingRegion", CellFunctions::IsChangingRegion},\ \ + {"GetMapTileCellX", CellFunctions::GetMapTileCellX},\ + {"GetMapTileCellY", CellFunctions::GetMapTileCellY},\ + {"SaveMapTileImageFile", CellFunctions::SaveMapTileImageFile},\ + \ {"SetCell", CellFunctions::SetCell},\ {"SetExteriorCell", CellFunctions::SetExteriorCell},\ \ - {"AddCellExplored", CellFunctions::AddCellExplored},\ + {"LoadMapTileImageFile", CellFunctions::LoadMapTileImageFile},\ \ {"SendCell", CellFunctions::SendCell},\ {"SendMapChanges", CellFunctions::SendMapChanges} @@ -50,6 +55,14 @@ public: */ 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. * @@ -118,6 +131,37 @@ public: */ 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. * @@ -147,13 +191,16 @@ public: 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 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 */ - 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. diff --git a/apps/openmw-mp/processors/player/ProcessorPlayerMap.hpp b/apps/openmw-mp/processors/player/ProcessorPlayerMap.hpp index 01d3cfe43..d3a985fb6 100644 --- a/apps/openmw-mp/processors/player/ProcessorPlayerMap.hpp +++ b/apps/openmw-mp/processors/player/ProcessorPlayerMap.hpp @@ -17,9 +17,7 @@ namespace mwmp { DEBUG_PRINTF(strPacketID.c_str()); - // Not currently implemented - // - // To be dealt with later to save explored areas on local maps + Script::Call(player.getId()); } }; } diff --git a/apps/openmw/mwbase/windowmanager.hpp b/apps/openmw/mwbase/windowmanager.hpp index 37e4d7050..6b731fbc4 100644 --- a/apps/openmw/mwbase/windowmanager.hpp +++ b/apps/openmw/mwbase/windowmanager.hpp @@ -213,9 +213,10 @@ namespace MWBase /* 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& imageData) = 0; /* End of tes3mp addition */ diff --git a/apps/openmw/mwgui/mapwindow.cpp b/apps/openmw/mwgui/mapwindow.cpp index ff389c797..84bd1be19 100644 --- a/apps/openmw/mwgui/mapwindow.cpp +++ b/apps/openmw/mwgui/mapwindow.cpp @@ -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& imageData) + { + mGlobalMapRender->setImage(cellX, cellY, imageData); + } + /* + End of tes3mp addition + */ + void MapWindow::updateCustomMarkers() { LocalMapBase::updateCustomMarkers(); diff --git a/apps/openmw/mwgui/mapwindow.hpp b/apps/openmw/mwgui/mapwindow.hpp index bb5546174..b1360268c 100644 --- a/apps/openmw/mwgui/mapwindow.hpp +++ b/apps/openmw/mwgui/mapwindow.hpp @@ -266,6 +266,17 @@ namespace MWGui void setGlobalMapPlayerPosition (float worldX, float worldY); 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& imageData); + /* + End of tes3mp addition + */ + void ensureGlobalMapLoaded(); virtual void onOpen(); diff --git a/apps/openmw/mwgui/windowmanagerimp.cpp b/apps/openmw/mwgui/windowmanagerimp.cpp index 1e7edb0ae..a36634542 100644 --- a/apps/openmw/mwgui/windowmanagerimp.cpp +++ b/apps/openmw/mwgui/windowmanagerimp.cpp @@ -1005,11 +1005,12 @@ namespace MWGui /* 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& imageData) { - mMap->cellExplored(cell->getCell()->getGridX(), cell->getCell()->getGridY()); + mMap->setGlobalMapImage(cellX, cellY, imageData); } /* End of tes3mp addition diff --git a/apps/openmw/mwgui/windowmanagerimp.hpp b/apps/openmw/mwgui/windowmanagerimp.hpp index 60d03a7d5..c153438e5 100644 --- a/apps/openmw/mwgui/windowmanagerimp.hpp +++ b/apps/openmw/mwgui/windowmanagerimp.hpp @@ -246,9 +246,10 @@ namespace MWGui /* 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& imageData); /* End of tes3mp addition */ diff --git a/apps/openmw/mwmp/LocalPlayer.cpp b/apps/openmw/mwmp/LocalPlayer.cpp index 94e88018d..bee4ea01b 100644 --- a/apps/openmw/mwmp/LocalPlayer.cpp +++ b/apps/openmw/mwmp/LocalPlayer.cpp @@ -1294,12 +1294,14 @@ void LocalPlayer::setMapExplored() MWWorld::Ptr ptrPlayer = getPlayerPtr(); 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) - MWBase::Environment::get().getWindowManager()->setCellExplored(ptrCellStore); + if (!cellStore->getCell()->mName.empty()) + 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(); } +void LocalPlayer::sendMapExplored(int x, int y, const std::vector& 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() { cellStateChanges.cellStates.clear(); diff --git a/apps/openmw/mwmp/LocalPlayer.hpp b/apps/openmw/mwmp/LocalPlayer.hpp index 741c9279f..c51645136 100644 --- a/apps/openmw/mwmp/LocalPlayer.hpp +++ b/apps/openmw/mwmp/LocalPlayer.hpp @@ -64,10 +64,10 @@ namespace mwmp void setFactions(); void setKills(); void setBooks(); - void setMapExplored(); void setShapeshift(); void setMarkLocation(); void setSelectedSpell(); + void setMapExplored(); void sendClass(); void sendInventory(); @@ -90,6 +90,7 @@ namespace mwmp void sendWerewolfState(bool isWerewolf); void sendMarkLocation(const ESM::Cell& newMarkCell, const ESM::Position& newMarkPosition); void sendSelectedSpell(const std::string& newSelectedSpellId); + void sendMapExplored(int x, int y, const std::vector& imageData); void clearCellStates(); void clearCurrentContainer(); diff --git a/apps/openmw/mwrender/globalmap.cpp b/apps/openmw/mwrender/globalmap.cpp index 24f6de6ce..52a878eb0 100644 --- a/apps/openmw/mwrender/globalmap.cpp +++ b/apps/openmw/mwrender/globalmap.cpp @@ -19,6 +19,18 @@ #include +/* + Start of tes3mp addition + + Include additional headers for multiplayer purposes +*/ +#include +#include "../mwmp/Main.hpp" +#include "../mwmp/LocalPlayer.hpp" +/* + End of tes3mp addition +*/ + #include "../mwbase/environment.hpp" #include "../mwbase/world.hpp" @@ -96,6 +108,17 @@ namespace 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 originToCellX; + std::map originToCellY; + /* + End of tes3mp addition + */ class CreateMapWorkItem : public SceneUtil::WorkItem { @@ -382,6 +405,17 @@ namespace MWRender if (cellX > mMaxX || cellX < mMinX || cellY > mMaxY || cellY < mMinY) 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); } @@ -598,6 +632,40 @@ namespace MWRender ensureLoaded(); 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 vectorData = std::vector(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); } } @@ -607,4 +675,50 @@ namespace MWRender cam->removeChildren(0, cam->getNumChildren()); 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& 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 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 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 + */ } diff --git a/apps/openmw/mwrender/globalmap.hpp b/apps/openmw/mwrender/globalmap.hpp index f19f4a91f..494bf1a3d 100644 --- a/apps/openmw/mwrender/globalmap.hpp +++ b/apps/openmw/mwrender/globalmap.hpp @@ -60,6 +60,17 @@ namespace MWRender 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& imageData); + /* + End of tes3mp addition + */ + /** * Mark a camera for cleanup in the next update. For internal use only. */ diff --git a/components/openmw-mp/Base/BasePlayer.hpp b/components/openmw-mp/Base/BasePlayer.hpp index 9484ff9f0..f625e1c0d 100644 --- a/components/openmw-mp/Base/BasePlayer.hpp +++ b/components/openmw-mp/Base/BasePlayer.hpp @@ -95,6 +95,13 @@ namespace mwmp int type; // 0 - Cell load, 1 - Cell unload }; + struct MapTile + { + int x; + int y; + std::vector imageData; + }; + struct JournalChanges { std::vector journalItems; @@ -136,8 +143,7 @@ namespace mwmp struct MapChanges { - std::vector cellsExplored; - unsigned int count; + std::vector mapTiles; }; struct InventoryChanges diff --git a/components/openmw-mp/Packets/Player/PacketPlayerMap.cpp b/components/openmw-mp/Packets/Player/PacketPlayerMap.cpp index 9027f55c9..5d65858c5 100644 --- a/components/openmw-mp/Packets/Player/PacketPlayerMap.cpp +++ b/components/openmw-mp/Packets/Player/PacketPlayerMap.cpp @@ -13,24 +13,40 @@ void PacketPlayerMap::Packet(RakNet::BitStream *bs, bool send) { PlayerPacket::Packet(bs, send); + uint32_t changesCount; + if (send) - player->mapChanges.count = (unsigned int)(player->mapChanges.cellsExplored.size()); - else - player->mapChanges.cellsExplored.clear(); + changesCount = static_cast(player->mapChanges.mapTiles.size()); - RW(player->mapChanges.count, send); + RW(changesCount, send); - for (unsigned int i = 0; i < player->mapChanges.count; i++) + if (!send) { - ESM::Cell cellExplored; + player->mapChanges.mapTiles.clear(); + player->mapChanges.mapTiles.resize(changesCount); + } + + for (auto &&mapTile : player->mapChanges.mapTiles) + { + RW(mapTile.x, send); + RW(mapTile.y, send); + + uint32_t imageDataCount; if (send) - cellExplored = player->mapChanges.cellsExplored.at(i); + imageDataCount = static_cast(mapTile.imageData.size()); - RW(cellExplored.mData, send, 1); - RW(cellExplored.mName, send, 1); + RW(imageDataCount, send); if (!send) - player->mapChanges.cellsExplored.push_back(cellExplored); + { + mapTile.imageData.clear(); + mapTile.imageData.resize(imageDataCount); + } + + for (auto &&imageChar : mapTile.imageData) + { + RW(imageChar, send); + } } }