Skip to content

Commit

Permalink
Merge pull request #107 from MafiaHub/scripting-split-human-player
Browse files Browse the repository at this point in the history
Scripting: Split Human and Player builtins
  • Loading branch information
Segfaultd authored Jan 9, 2025
2 parents bc89382 + fcbe96b commit b4dfb16
Show file tree
Hide file tree
Showing 17 changed files with 226 additions and 183 deletions.
12 changes: 6 additions & 6 deletions code/client/src/core/modules/human.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "sdk/ue/game/camera/c_game_camera.h"
#include "sdk/wrappers/c_human_2_car_wrapper.h"

#include "shared/game_rpc/add_weapon.h"
#include "shared/game_rpc/human/human_add_weapon.h"
#include "shared/game_rpc/human/human_changeskin.h"
#include "shared/game_rpc/human/human_reload.h"
#include "shared/game_rpc/human/human_setprops.h"
Expand Down Expand Up @@ -165,8 +165,8 @@ namespace MafiaMP::Core::Modules {
}

void Human::Create(flecs::entity e, uint64_t spawnProfile) {
auto info = Core::gApplication->GetEntityFactory()->RequestHuman(spawnProfile);
auto &trackingData = e.ensure<Core::Modules::Human::Tracking>();
auto info = Core::gApplication->GetEntityFactory()->RequestHuman(spawnProfile);
auto &trackingData = e.ensure<Core::Modules::Human::Tracking>();
trackingData.info = info;
trackingData.human = nullptr;

Expand Down Expand Up @@ -229,7 +229,7 @@ namespace MafiaMP::Core::Modules {
}

void Human::SetupLocalPlayer(Application *, flecs::entity e) {
auto &trackingData = e.ensure<Core::Modules::Human::Tracking>();
auto &trackingData = e.ensure<Core::Modules::Human::Tracking>();
trackingData.human = Game::Helpers::Controls::GetLocalPlayer();
trackingData.info = nullptr;

Expand All @@ -240,7 +240,7 @@ namespace MafiaMP::Core::Modules {
e.set<Shared::Modules::Mod::EntityKind>({Shared::Modules::Mod::MOD_PLAYER});
e.add<Framework::World::Modules::Base::Frame>();

auto es = e.get_mut<Framework::World::Modules::Base::Streamable>();
auto es = e.get_mut<Framework::World::Modules::Base::Streamable>();
es->modEvents.updateProc = [](Framework::Networking::NetworkPeer *peer, uint64_t guid, flecs::entity e) {
const auto updateData = e.get<Shared::Modules::HumanSync::UpdateData>();

Expand Down Expand Up @@ -499,7 +499,7 @@ namespace MafiaMP::Core::Modules {
}
});

net->RegisterGameRPC<Shared::RPC::AddWeapon>([app](SLNet::RakNetGUID guid, Shared::RPC::AddWeapon *msg) {
net->RegisterGameRPC<Shared::RPC::HumanAddWeapon>([app](SLNet::RakNetGUID guid, Shared::RPC::HumanAddWeapon *msg) {
if (!msg->Valid())
return;

Expand Down
1 change: 1 addition & 0 deletions code/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ set(MAFIAMP_SERVER_FILES
src/main.cpp
src/core/server.cpp

src/core/builtins/human.cpp
src/core/builtins/player.cpp
src/core/builtins/vehicle.cpp

Expand Down
2 changes: 2 additions & 0 deletions code/server/src/core/builtins/builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "scripting/server_engine.h"

#include "chat.h"
#include "human.h"
#include "player.h"
#include "vehicle.h"
#include "world.h"
Expand All @@ -15,6 +16,7 @@ namespace MafiaMP::Scripting {
static void Register(sol::state &luaEngine) {
Scripting::Chat::Register(luaEngine);
Scripting::Human::Register(luaEngine);
Scripting::Player::Register(luaEngine);
Scripting::Vehicle::Register(luaEngine);
Scripting::World::Register(luaEngine);
}
Expand Down
40 changes: 19 additions & 21 deletions code/server/src/core/builtins/chat.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,45 @@

#include <sol/sol.hpp>

#include "scripting/server_engine.h"
#include "core/server.h"

#include "shared/rpc/chat_message.h"

#include "player.h"

#include "core_modules.h"

namespace MafiaMP::Scripting {
class Chat final {
public:
static void SendToPlayer(Human *human, std::string message) {
if (human) {
const auto ent = human->GetHandle();
const auto str = ent.get<Framework::World::Modules::Base::Streamer>();

if (!str)
return;
static void EventChatMessage(flecs::entity e, std::string message) {
const auto engine = MafiaMP::Server::GetScriptingEngine();
engine->InvokeEvent("onChatMessage", Player(e), message);
}

FW_SEND_COMPONENT_RPC_TO(Shared::RPC::ChatMessage, SLNet::RakNetGUID(str->guid), message);
}
static void EventChatCommand(flecs::entity e, std::string message, std::string command, std::vector<std::string> args) {
const auto engine = MafiaMP::Server::GetScriptingEngine();
engine->InvokeEvent("onChatCommand", Player(e), message, command, args);
}

static void SendToAll(std::string message) {
FW_SEND_COMPONENT_RPC(Shared::RPC::ChatMessage, message);
}

static void EventChatMessage(flecs::entity e, std::string message) {
const auto engine = MafiaMP::Server::GetScriptingEngine();
engine->InvokeEvent("onChatMessage", Human(e), message);
}
static void SendToPlayer(Player *player, std::string message) {
if (player) {
const auto ent = player->GetHandle();
const auto str = ent.get<Framework::World::Modules::Base::Streamer>();

static void EventChatCommand(flecs::entity e, std::string message, std::string command, std::vector<std::string> args) {
const auto engine = MafiaMP::Server::GetScriptingEngine();
engine->InvokeEvent("onChatCommand", Human(e), message, command, args);
if (!str)
return;

FW_SEND_COMPONENT_RPC_TO(Shared::RPC::ChatMessage, SLNet::RakNetGUID(str->guid), message);
}
}

static void Register(sol::state &luaEngine) {
sol::usertype<Chat> cls = luaEngine.new_usertype<Chat>("Chat");
cls["sendToPlayer"] = &Chat::SendToPlayer;
cls["sendToAll"] = &Chat::SendToAll;
cls["sendToAll"] = &Chat::SendToAll;
cls["sendToPlayer"] = &Chat::SendToPlayer;
}
};
} // namespace MafiaMP::Scripting
62 changes: 62 additions & 0 deletions code/server/src/core/builtins/human.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "human.h"

#include "core/server.h"

#include "shared/game_rpc/human/human_add_weapon.h"
#include "shared/game_rpc/human/human_setprops.h"

#include "shared/modules/human_sync.hpp"

#include "vehicle.h"

namespace MafiaMP::Scripting {
std::string Human::ToString() const {
std::ostringstream ss;
ss << "Human{ id: " << _ent.id() << " }";
return ss.str();
}

void Human::AddWeapon(int weaponId, int ammo) {
FW_SEND_SERVER_COMPONENT_GAME_RPC(MafiaMP::Shared::RPC::HumanAddWeapon, _ent, weaponId, ammo);
}

float Human::GetHealth() const {
auto h = _ent.get<MafiaMP::Shared::Modules::HumanSync::UpdateData>();
return h->_healthPercent;
}

void Human::SetHealth(float health) {
auto h = _ent.get_mut<MafiaMP::Shared::Modules::HumanSync::UpdateData>();
h->_healthPercent = health;
MafiaMP::Shared::RPC::HumanSetProps msg {};
msg.health = health;
FW_SEND_SERVER_COMPONENT_GAME_RPC(MafiaMP::Shared::RPC::HumanSetProps, _ent, msg);
}

Vehicle Human::GetVehicle() const {
const auto updateData = _ent.get<MafiaMP::Shared::Modules::HumanSync::UpdateData>();
const auto carEnt = flecs::entity(_ent.world(), updateData->carPassenger.carId);
if (carEnt.is_valid() && carEnt.is_alive()) {
return Vehicle(carEnt);
}
return Vehicle(-1);
}

int Human::GetVehicleSeat() const {
const auto updateData = _ent.get<MafiaMP::Shared::Modules::HumanSync::UpdateData>();
const auto carEnt = flecs::entity(_ent.world(), updateData->carPassenger.carId);
if (carEnt.is_valid() && carEnt.is_alive()) {
return updateData->carPassenger.seatId;
}
return -1;
}

void Human::Register(sol::state &luaEngine) {
sol::usertype<Human> cls = luaEngine.new_usertype<Human>("Human", sol::constructors<Human(uint64_t)>(), sol::base_classes, sol::bases<Entity>());
cls["addWeapon"] = &Human::AddWeapon;
cls["getHealth"] = &Human::GetHealth;
cls["setHealth"] = &Human::SetHealth;
cls["getVehicle"] = &Human::GetVehicle;
cls["getVehicleSeat"] = &Human::GetVehicleSeat;
}
} // namespace MafiaMP::Scripting
37 changes: 37 additions & 0 deletions code/server/src/core/builtins/human.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <sol/sol.hpp>

#include "integrations/server/scripting/builtins/entity.h"

#include "shared/modules/human_sync.hpp"

namespace MafiaMP::Scripting {
class Vehicle;
class Human: public Framework::Integrations::Scripting::Entity {
public:
Human(flecs::entity_t ent): Entity(ent) {
const auto humanData = _ent.get<Shared::Modules::HumanSync::UpdateData>();

if (!humanData) {
throw std::runtime_error(fmt::format("Entity handle '{}' is not a Human!", ent));
}
}

Human(flecs::entity ent): Human(ent.id()) {}

std::string ToString() const override;

void AddWeapon(int weaponId, int ammo);

float GetHealth() const;

void SetHealth(float health);

Vehicle GetVehicle() const;

int GetVehicleSeat() const;

static void Register(sol::state &luaEngine);
};
} // namespace MafiaMP::Scripting
96 changes: 27 additions & 69 deletions code/server/src/core/builtins/player.cpp
Original file line number Diff line number Diff line change
@@ -1,91 +1,49 @@

#include "player.h"

#include "vehicle.h"
#include "core/server.h"

#include "shared/modules/human_sync.hpp"
#include "shared/game_rpc/add_weapon.h"
#include "shared/game_rpc/human/human_setprops.h"
#include "shared/rpc/chat_message.h"

namespace MafiaMP::Scripting {
std::string Human::ToString() const {
std::ostringstream ss;
ss << "Human{ id: " << _ent.id() << " }";
return ss.str();
void Player::EventPlayerConnected(flecs::entity e) {
const auto engine = MafiaMP::Server::GetScriptingEngine();
engine->InvokeEvent("onPlayerConnected", Player(e));
}

void Human::Destroy() {
// Nothing should happen here, as the entity is destroyed by the game and network systems
void Player::EventPlayerDisconnected(flecs::entity e) {
const auto engine = MafiaMP::Server::GetScriptingEngine();
engine->InvokeEvent("onPlayerDisconnected", Player(e));
}

void Human::AddWeapon(int weaponId, int ammo) {
FW_SEND_SERVER_COMPONENT_GAME_RPC(MafiaMP::Shared::RPC::AddWeapon, _ent, weaponId, ammo);
void Player::EventPlayerDied(flecs::entity e) {
const auto engine = MafiaMP::Server::GetScriptingEngine();
engine->InvokeEvent("onPlayerDied", Player(e));
}

void Human::SendChat(std::string message) {
const auto str = _ent.get<Framework::World::Modules::Base::Streamer>();
FW_SEND_COMPONENT_RPC_TO(Shared::RPC::ChatMessage, SLNet::RakNetGUID(str->guid), message);
std::string Player::ToString() const {
std::ostringstream ss;
ss << "Player{ id: " << _ent.id() << " }";
return ss.str();
}

Vehicle Human::GetVehicle() const {
const auto updateData = _ent.get<MafiaMP::Shared::Modules::HumanSync::UpdateData>();
const auto carEnt = flecs::entity(_ent.world(), updateData->carPassenger.carId);
if (carEnt.is_valid() && carEnt.is_alive()) {
return Vehicle(carEnt);
}
return Vehicle(-1);
void Player::Destroy() {
// Nothing should happen here, as the player entity is destroyed by the game and network systems
}

int Human::GetVehicleSeat() const {
const auto updateData = _ent.get<MafiaMP::Shared::Modules::HumanSync::UpdateData>();
const auto carEnt = flecs::entity(_ent.world(), updateData->carPassenger.carId);
if (carEnt.is_valid() && carEnt.is_alive()) {
return updateData->carPassenger.seatId;
}
return -1;
void Player::SendChat(std::string message) {
const auto str = _ent.get<Framework::World::Modules::Base::Streamer>();
FW_SEND_COMPONENT_RPC_TO(Shared::RPC::ChatMessage, SLNet::RakNetGUID(str->guid), message);
}

void Human::SendChatToAll(std::string message) {
void Player::SendChatToAll(std::string message) {
FW_SEND_COMPONENT_RPC(Shared::RPC::ChatMessage, message);
}

void Human::SetHealth(float health) {
auto h = _ent.get_mut<MafiaMP::Shared::Modules::HumanSync::UpdateData>();
h->_healthPercent = health;
MafiaMP::Shared::RPC::HumanSetProps msg {};
msg.health = health;
FW_SEND_SERVER_COMPONENT_GAME_RPC(MafiaMP::Shared::RPC::HumanSetProps, _ent, msg);
}

float Human::GetHealth() const {
auto h = _ent.get<MafiaMP::Shared::Modules::HumanSync::UpdateData>();
return h->_healthPercent;
}

void Human::EventPlayerDied(flecs::entity e) {
const auto engine = MafiaMP::Server::GetScriptingEngine();
engine->InvokeEvent("onPlayerDied", Human(e));
}

void Human::EventPlayerConnected(flecs::entity e) {
const auto engine = MafiaMP::Server::GetScriptingEngine();
engine->InvokeEvent("onPlayerConnected", Human(e));
}

void Human::EventPlayerDisconnected(flecs::entity e) {
const auto engine = MafiaMP::Server::GetScriptingEngine();
engine->InvokeEvent("onPlayerDisconnected", Human(e));
}

void Human::Register(sol::state &luaEngine) {
sol::usertype<Human> cls = luaEngine.new_usertype<Human>("Human", sol::constructors<Human(uint64_t)>(), sol::base_classes, sol::bases<Entity>());
cls["destroy"] = &Human::Destroy;
cls["addWeapon"] = &Human::AddWeapon;
cls["setHealth"] = &Human::SetHealth;
cls["getHealth"] = &Human::GetHealth;
cls["getVehicle"] = &Human::GetVehicle;
cls["getVehicleSeat"] = &Human::GetVehicleSeat;
cls["sendChat"] = &Human::SendChat;
cls["sendChatToAll"] = &Human::SendChatToAll;
void Player::Register(sol::state &luaEngine) {
sol::usertype<Player> cls = luaEngine.new_usertype<Player>("Player", sol::constructors<Player(uint64_t)>(), sol::base_classes, sol::bases<Human, Entity>());
cls["destroy"] = &Player::Destroy;
cls["sendChat"] = &Player::SendChat;
cls["sendChatToAll"] = &Player::SendChatToAll;
}
}
} // namespace MafiaMP::Scripting
Loading

0 comments on commit b4dfb16

Please sign in to comment.