Skip to content

Commit a02296b

Browse files
add item count syncing
1 parent d5d59c9 commit a02296b

4 files changed

Lines changed: 37 additions & 0 deletions

File tree

src/game_party.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ void Game_Party::AddItem(int item_id, int amount) {
201201
}
202202

203203
int total_items = data.item_counts[idx] + amount;
204+
GMI().ItemSet(item_id, total_items);
204205

205206
if (total_items <= 0) {
206207
data.item_ids.erase(data.item_ids.begin() + idx);

src/multiplayer/game_multiplayer.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ void Game_Multiplayer::InitConnection() {
213213
sync_action_events.push_back(p.event_id);
214214
}
215215
});
216+
connection.RegisterHandler<SyncItemPacket>("si", [this] (SyncItemPacket& p) {
217+
int count = (int) Main_Data::game_party->GetItemCount(p.item_id);
218+
if (p.sync_type != -1) {
219+
connection.SendPacketAsync<Messages::C2S::SyncItemPacket>(p.item_id, count);
220+
}
221+
if (p.sync_type >= -1) {
222+
sync_items.push_back(p.item_id);
223+
}
224+
});
216225
connection.RegisterHandler<SyncPicturePacket>("sp", [this] (SyncPicturePacket& p) {
217226
sync_picture_names.push_back(p.picture_name);
218227
});
@@ -516,6 +525,7 @@ void Game_Multiplayer::Initialize() {
516525
players.clear();
517526
sync_switches.clear();
518527
sync_vars.clear();
528+
sync_items.clear();
519529
sync_events.clear();
520530
sync_action_events.clear();
521531
sync_picture_names.clear();
@@ -775,6 +785,12 @@ void Game_Multiplayer::VariableSet(int var_id, int value) {
775785
}
776786
}
777787

788+
void Game_Multiplayer::ItemSet(int item_id, int count) {
789+
if (std::find(sync_items.begin(), sync_items.end(), item_id) != sync_items.end()) {
790+
connection.SendPacketAsync<Messages::C2S::SyncItemPacket>(item_id, count);
791+
}
792+
}
793+
778794
void Game_Multiplayer::ApplyScreenTone() {
779795
ApplyTone(Main_Data::game_screen->GetTone());
780796
}

src/multiplayer/game_multiplayer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Game_Multiplayer {
4848
void ApplyScreenTone();
4949
void SwitchSet(int switch_id, int value);
5050
void VariableSet(int var_id, int value);
51+
void ItemSet(int item_id, int count);
5152

5253
struct {
5354
bool enable_sounds{ true };
@@ -82,6 +83,7 @@ class Game_Multiplayer {
8283
std::vector<PlayerOther> dc_players;
8384
std::vector<int> sync_switches;
8485
std::vector<int> sync_vars;
86+
std::vector<int> sync_items;
8587
std::vector<int> sync_events;
8688
std::vector<int> sync_action_events;
8789
std::vector<std::string> sync_picture_names; // for badge conditions

src/multiplayer/messages.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,15 @@ namespace S2C {
345345
const int trigger_type;
346346
};
347347

348+
class SyncItemPacket : public S2CPacket {
349+
public:
350+
SyncItemPacket(const PL& v)
351+
: item_id(Decode<int>(v.at(0))), sync_type(Decode<int>(v.at(1))) {}
352+
353+
const int item_id;
354+
const int sync_type;
355+
};
356+
348357
class SyncPicturePacket : public S2CPacket {
349358
public:
350359
SyncPicturePacket(const PL& v)
@@ -638,6 +647,15 @@ namespace C2S {
638647
int action_bin;
639648
};
640649

650+
class SyncItemPacket : public C2SPacket {
651+
public:
652+
SyncItemPacket(int _item_id, int _count) : C2SPacket("si"),
653+
item_id(_item_id), count(_count) {}
654+
std::string ToBytes() const override { return Build(item_id, count); }
655+
protected:
656+
int item_id;
657+
int count;
658+
};
641659
}
642660
}
643661

0 commit comments

Comments
 (0)