-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from MafiaHub/scripting-split-human-player
Scripting: Split Human and Player builtins
- Loading branch information
Showing
17 changed files
with
226 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.