Skip to content

Commit 7d2d65a

Browse files
Add multiplayer XP sharing
1 parent 8428d01 commit 7d2d65a

File tree

1 file changed

+63
-22
lines changed

1 file changed

+63
-22
lines changed

main.cpp

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,32 @@ class Mod : GenericMod {
5858
};
5959

6060
std::vector<cube::TextFX> m_FXList;
61+
62+
void GainXP(cube::Game* game, int xp)
63+
{
64+
FloatRGBA purple(0.65f, 0.40f, 1.0f, 1.0f);
65+
wchar_t buffer[250];
66+
swprintf_s(buffer, 250, L"You gain %d xp.\n", xp);
67+
game->PrintMessage(buffer, &purple);
68+
69+
cube::TextFX xpText = cube::TextFX();
70+
xpText.position = game->GetPlayer()->entity_data.position + LongVector3(std::rand() % (cube::DOTS_PER_BLOCK * 50), std::rand() % (cube::DOTS_PER_BLOCK * 50), std::rand() % (cube::DOTS_PER_BLOCK * 50));
71+
xpText.animation_length = 3000;
72+
xpText.distance_to_fall = -100;
73+
xpText.color = purple;
74+
xpText.size = 32;
75+
xpText.offset_2d = FloatVector2(-50, -100);
76+
xpText.text = std::wstring(L"+") + std::to_wstring(xp) + std::wstring(L" XP");;
77+
xpText.field_60 = 0;
78+
79+
for (int i = 0; i < 4; i++)
80+
{
81+
m_FXList.push_back(xpText);
82+
}
83+
84+
game->GetPlayer()->entity_data.XP += xp;
85+
}
86+
6187
/* Hook for the chat function. Triggers when a user sends something in the chat.
6288
* @param {std::wstring*} message
6389
* @return {int}
@@ -162,6 +188,24 @@ class Mod : GenericMod {
162188
}
163189
}
164190

191+
uint32 size = 0;
192+
if (cube::SteamNetworking()->IsP2PPacketAvailable(&size, 2))
193+
{
194+
u8 buffer[size];
195+
CSteamID id;
196+
cube::SteamNetworking()->ReadP2PPacket(&buffer, size, &size, &id, 2);
197+
198+
BytesIO package(buffer, size);
199+
200+
int pkg_id = package.Read<int>();
201+
int xp = package.Read<int>();
202+
203+
if (pkg_id == 0x1)
204+
{
205+
GainXP(game, xp);
206+
}
207+
}
208+
165209
return;
166210
}
167211

@@ -189,10 +233,10 @@ class Mod : GenericMod {
189233
return;
190234
}
191235

192-
cube::Creature* player = game->GetPlayer();
193-
if (player->id == attacker->id || player->pet_id == attacker->id)
236+
if (attacker->entity_data.hostility_type == cube::Creature::EntityBehaviour::Player ||
237+
attacker->entity_data.hostility_type == cube::Creature::EntityBehaviour::Pet)
194238
{
195-
FloatRGBA purple(0.65f, 0.40f, 1.0f, 1.0f);
239+
196240
int xp_gain = GetCreatureLevel(creature) * (creature->entity_data.level + 1);
197241

198242
if ((creature->entity_data.appearance.flags2 & (1 << (int)cube::Creature::AppearanceModifiers::IsBoss)) != 0)
@@ -210,26 +254,23 @@ class Mod : GenericMod {
210254
xp_gain *= 2;
211255
}
212256

213-
wchar_t buffer[250];
214-
swprintf_s(buffer, 250, L"You gain %d xp.\n", xp_gain);
215-
game->PrintMessage(buffer, &purple);
216-
217-
cube::TextFX xpText = cube::TextFX();
218-
xpText.position = player->entity_data.position + LongVector3(std::rand() % (cube::DOTS_PER_BLOCK * 50) , std::rand() % (cube::DOTS_PER_BLOCK * 50), std::rand() % (cube::DOTS_PER_BLOCK * 50));
219-
xpText.animation_length = 3000;
220-
xpText.distance_to_fall = -100;
221-
xpText.color = purple;
222-
xpText.size = 32;
223-
xpText.offset_2d = FloatVector2(-50, -100);
224-
xpText.text = std::wstring(L"+") + std::to_wstring(xp_gain) + std::wstring(L" XP");;
225-
xpText.field_60 = 0;
226-
227-
for (int i = 0; i < 4; i++)
228-
{
229-
m_FXList.push_back(xpText);
230-
}
231257

232-
player->entity_data.XP += xp_gain;
258+
// Code taken and modified from: https://github.com/ChrisMiuchiz/Cube-World-Chat-Mod/blob/master/main.cpp
259+
// Send a packet instead of just writing to chat, if you are in an online session
260+
// An online session shall be defined as whether you're connected to someone else's steam ID, or if your host has more than 1 connection
261+
if (cube::SteamUser()->GetSteamID() == game->client.host_steam_id || game->host.connections.size() >= 1) {
262+
// Construct a chat packet
263+
// Thanks to Andoryuuta for reverse engineering these packets, even before the beta was released
264+
BytesIO bytesio;
265+
266+
bytesio.Write<u32>(0x01); //Packet ID
267+
bytesio.Write<u32>(xp_gain / game->host.connections.size()); //Message size
268+
269+
for (auto& conn : game->host.connections)
270+
{
271+
cube::SteamNetworking()->SendP2PPacket(conn.first, bytesio.Data(), bytesio.Size(), k_EP2PSendReliable, 2);
272+
}
273+
}
233274
}
234275
}
235276

0 commit comments

Comments
 (0)