Skip to content

Commit a60b907

Browse files
Merge branch 'master' into master
2 parents 7a5608f + 7d2d65a commit a60b907

File tree

1 file changed

+62
-24
lines changed

1 file changed

+62
-24
lines changed

main.cpp

Lines changed: 62 additions & 24 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}
@@ -161,6 +187,24 @@ class Mod : GenericMod {
161187
}
162188
}
163189

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

@@ -188,11 +232,9 @@ class Mod : GenericMod {
188232
return;
189233
}
190234

191-
cube::Creature* player = game->GetPlayer();
192-
if (player->id == attacker->id || player->pet_id == attacker->id)
235+
if (attacker->entity_data.hostility_type == cube::Creature::EntityBehaviour::Player ||
236+
attacker->entity_data.hostility_type == cube::Creature::EntityBehaviour::Pet)
193237
{
194-
FloatRGBA purple(0.65f, 0.40f, 1.0f, 1.0f);
195-
196238
int xp_gain = 100;
197239

198240
if (player->entity_data.level > GetCreatureLevel(creature)) {
@@ -202,7 +244,6 @@ class Mod : GenericMod {
202244
xp_gain = (int)std::roundf(100 * (creature->entity_data.level + 1) * (1 + 0.15f * (GetCreatureLevel(creature) - player->entity_data.level)) * std::powf(1.05f, GetCreatureLevel(creature) - player->entity_data.level));
203245
}
204246

205-
206247
if ((creature->entity_data.appearance.flags2 & (1 << (int)cube::Creature::AppearanceModifiers::IsBoss)) != 0)
207248
{
208249
xp_gain *= 10;
@@ -218,26 +259,23 @@ class Mod : GenericMod {
218259
xp_gain *= 2;
219260
}
220261

221-
wchar_t buffer[250];
222-
swprintf_s(buffer, 250, L"You gain %d xp.\n", xp_gain);
223-
game->PrintMessage(buffer, &purple);
224-
225-
cube::TextFX xpText = cube::TextFX();
226-
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));
227-
xpText.animation_length = 3000;
228-
xpText.distance_to_fall = -100;
229-
xpText.color = purple;
230-
xpText.size = 32;
231-
xpText.offset_2d = FloatVector2(-50, -100);
232-
xpText.text = std::wstring(L"+") + std::to_wstring(xp_gain) + std::wstring(L" XP");;
233-
xpText.field_60 = 0;
234-
235-
for (int i = 0; i < 4; i++)
236-
{
237-
m_FXList.push_back(xpText);
238-
}
239262

240-
player->entity_data.XP += xp_gain;
263+
// Code taken and modified from: https://github.com/ChrisMiuchiz/Cube-World-Chat-Mod/blob/master/main.cpp
264+
// Send a packet instead of just writing to chat, if you are in an online session
265+
// 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
266+
if (cube::SteamUser()->GetSteamID() == game->client.host_steam_id || game->host.connections.size() >= 1) {
267+
// Construct a chat packet
268+
// Thanks to Andoryuuta for reverse engineering these packets, even before the beta was released
269+
BytesIO bytesio;
270+
271+
bytesio.Write<u32>(0x01); //Packet ID
272+
bytesio.Write<u32>(xp_gain / game->host.connections.size()); //Message size
273+
274+
for (auto& conn : game->host.connections)
275+
{
276+
cube::SteamNetworking()->SendP2PPacket(conn.first, bytesio.Data(), bytesio.Size(), k_EP2PSendReliable, 2);
277+
}
278+
}
241279
}
242280
}
243281

0 commit comments

Comments
 (0)