From 784c42f5f97c1f6dd79114041e31a01a7bbbde7e Mon Sep 17 00:00:00 2001 From: flan Date: Sun, 3 Aug 2025 21:28:42 +0200 Subject: [PATCH 1/2] Make picture packet handlers use consistent player offsets Game_Multiplayer packet handlers had a hardcoded offset of 50, but Game_Pictures::EraseAllMultiplayer used the offset determined by the target RPG_RT version instead, which could've been higher or lower than the assumed 50. Prevents other players' pictures from potentially not being cleaned up when they disconnect. For games with support for >50 pictures this also prevents remote pictures from accidentally overwriting local pictures with IDs over 50. --- src/game_pictures.cpp | 6 +++++- src/game_pictures.h | 1 + src/multiplayer/game_multiplayer.cpp | 6 +++--- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/game_pictures.cpp b/src/game_pictures.cpp index e58b0d34d..da1457698 100644 --- a/src/game_pictures.cpp +++ b/src/game_pictures.cpp @@ -153,6 +153,10 @@ int Game_Pictures::GetDefaultNumberOfPictures() { return 0; } +int Game_Pictures::GetPictureIdForPlayer(int player_id, int pic_id) { + return (player_id + 1) * GetDefaultNumberOfPictures() + pic_id; +} + Game_Pictures::Picture& Game_Pictures::GetPicture(int id) { if (EP_UNLIKELY(id > static_cast(pictures.size()))) { pictures.reserve(id); @@ -366,7 +370,7 @@ void Game_Pictures::EraseAllMultiplayer() { } void Game_Pictures::EraseAllMultiplayerForPlayer(int id) { - auto start = (id + 1) * GetDefaultNumberOfPictures() + 1; + auto start = GetPictureIdForPlayer(id, 1); auto end = start + GetDefaultNumberOfPictures(); for (auto& pic: pictures) { if (pic.data.ID >= start && pic.data.ID < end) { diff --git a/src/game_pictures.h b/src/game_pictures.h index 9fb432aa5..2f92a2151 100644 --- a/src/game_pictures.h +++ b/src/game_pictures.h @@ -42,6 +42,7 @@ class Game_Pictures { void InitGraphics(); static int GetDefaultNumberOfPictures(); + static int GetPictureIdForPlayer(int player_id, int pic_id); struct Params { int position_x = 0; diff --git a/src/multiplayer/game_multiplayer.cpp b/src/multiplayer/game_multiplayer.cpp index bbda99587..b119d5d17 100644 --- a/src/multiplayer/game_multiplayer.cpp +++ b/src/multiplayer/game_multiplayer.cpp @@ -416,18 +416,18 @@ void Game_Multiplayer::InitConnection() { connection.RegisterHandler("ap", [this, modify_args] (ShowPicturePacket& p) { if (players.find(p.id) == players.end()) return; modify_args(p); - int pic_id = p.pic_id + (p.id + 1) * 50; //offset to avoid conflicting with others using the same picture + int pic_id = Game_Pictures::GetPictureIdForPlayer(p.id, p.pic_id); Main_Data::game_pictures->Show(pic_id, p.params); }); connection.RegisterHandler("mp", [this, modify_args] (MovePicturePacket& p) { if (players.find(p.id) == players.end()) return; - int pic_id = p.pic_id + (p.id + 1) * 50; //offset to avoid conflicting with others using the same picture + int pic_id = Game_Pictures::GetPictureIdForPlayer(p.id, p.pic_id); modify_args(p); Main_Data::game_pictures->Move(pic_id, p.params); }); connection.RegisterHandler("rp", [this] (ErasePicturePacket& p) { if (players.find(p.id) == players.end()) return; - int pic_id = p.pic_id + (p.id + 1) * 50; //offset to avoid conflicting with others using the same picture + int pic_id = Game_Pictures::GetPictureIdForPlayer(p.id, p.pic_id); Main_Data::game_pictures->Erase(pic_id); }); connection.RegisterHandler("ba", [this] (ShowPlayerBattleAnimPacket& p) { From 67a8a7a0c05f0a34fbb30d9933fc0c18f226fa14 Mon Sep 17 00:00:00 2001 From: flan Date: Sun, 3 Aug 2025 21:30:57 +0200 Subject: [PATCH 2/2] Avoid saving remote pictures from players with ID < 2 The relaxed offset was introduced in 44af062ab5706c89b500bf1c, but the issue it was intended to fix (non-remote pictures not getting saved) seems to have been caused by something else, possibly the uninitialized idx variable. --- src/game_pictures.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/game_pictures.cpp b/src/game_pictures.cpp index da1457698..34c4aa83a 100644 --- a/src/game_pictures.cpp +++ b/src/game_pictures.cpp @@ -103,9 +103,8 @@ std::vector Game_Pictures::GetSaveData() const { // but in multiplayer we don't want to save pictures generated by other players // because 1) they occupy needless space and 2) they will be immediately out of date constexpr int min_pic_id = 1; - constexpr int min_player_id = 1; - // the 50 comes from the player image offset used for remote ShowPicture commands - const int multiplayer_pic_ids_begin = data_size + min_pic_id + (min_player_id + 1) * 50; + constexpr int min_player_id = 0; + const int multiplayer_pic_ids_begin = GetPictureIdForPlayer(min_player_id, min_pic_id); save.reserve(multiplayer_pic_ids_begin); for (const auto& pic: pictures) {