Skip to content

Commit e0d02c1

Browse files
add item count syncing
1 parent b998167 commit e0d02c1

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
@@ -202,6 +202,7 @@ void Game_Party::AddItem(int item_id, int amount) {
202202
}
203203

204204
int total_items = data.item_counts[idx] + amount;
205+
GMI().ItemSet(item_id, total_items);
205206

206207
if (total_items <= 0) {
207208
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
});
@@ -535,6 +544,7 @@ void Game_Multiplayer::Initialize() {
535544
players.clear();
536545
sync_switches.clear();
537546
sync_vars.clear();
547+
sync_items.clear();
538548
sync_events.clear();
539549
sync_action_events.clear();
540550
sync_picture_names.clear();
@@ -767,6 +777,12 @@ void Game_Multiplayer::VariableSet(int var_id, int value) {
767777
}
768778
}
769779

780+
void Game_Multiplayer::ItemSet(int item_id, int count) {
781+
if (std::find(sync_items.begin(), sync_items.end(), item_id) != sync_items.end()) {
782+
connection.SendPacketAsync<Messages::C2S::SyncItemPacket>(item_id, count);
783+
}
784+
}
785+
770786
void Game_Multiplayer::ApplyScreenTone() {
771787
ApplyTone(Main_Data::game_screen->GetTone());
772788
}

src/multiplayer/game_multiplayer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Game_Multiplayer {
4747
void ApplyScreenTone();
4848
void SwitchSet(int switch_id, int value);
4949
void VariableSet(int var_id, int value);
50+
void ItemSet(int item_id, int count);
5051
void UpdateNBPlayers();
5152
void UpdateCUTime();
5253
void UpdateCUWeather();
@@ -101,6 +102,7 @@ class Game_Multiplayer {
101102
std::vector<PlayerOther> dc_players;
102103
std::vector<int> sync_switches;
103104
std::vector<int> sync_vars;
105+
std::vector<int> sync_items;
104106
std::vector<int> sync_events;
105107
std::vector<int> sync_action_events;
106108
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
@@ -331,6 +331,15 @@ namespace S2C {
331331
const int trigger_type;
332332
};
333333

334+
class SyncItemPacket : public S2CPacket {
335+
public:
336+
SyncItemPacket(const PL& v)
337+
: item_id(Decode<int>(v.at(0))), sync_type(Decode<int>(v.at(1))) {}
338+
339+
const int item_id;
340+
const int sync_type;
341+
};
342+
334343
class SyncPicturePacket : public S2CPacket {
335344
public:
336345
SyncPicturePacket(const PL& v)
@@ -616,6 +625,15 @@ namespace C2S {
616625
int action_bin;
617626
};
618627

628+
class SyncItemPacket : public C2SPacket {
629+
public:
630+
SyncItemPacket(int _item_id, int _count) : C2SPacket("si"),
631+
item_id(_item_id), count(_count) {}
632+
std::string ToBytes() const override { return Build(item_id, count); }
633+
protected:
634+
int item_id;
635+
int count;
636+
};
619637
}
620638
}
621639

0 commit comments

Comments
 (0)