Skip to content

Commit

Permalink
server: Add vehicle passengers getter
Browse files Browse the repository at this point in the history
  • Loading branch information
xLuxy committed Sep 3, 2023
1 parent d495dc3 commit 72fadbc
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
65 changes: 65 additions & 0 deletions server/src/CNodeResourceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ void CNodeResourceImpl::OnEvent(const alt::CEvent* e)
v8::Context::Scope scope(GetContext());
// env->PushAsyncCallbackScope();

HandleVehiclePassengerSeatEvents(e);

V8Helpers::EventHandler* handler = V8Helpers::EventHandler::Get(e);
if(!handler) return;

Expand Down Expand Up @@ -186,6 +188,69 @@ void CNodeResourceImpl::OnEvent(const alt::CEvent* e)
return;
}

void CNodeResourceImpl::HandleVehiclePassengerSeatEvents(const alt::CEvent* ev)
{
auto evType = ev->GetType();

if (evType == alt::CEvent::Type::PLAYER_ENTER_VEHICLE)
{
auto event = static_cast<const alt::CPlayerEnterVehicleEvent*>(ev);
auto vehicle = event->GetTarget();
auto player = event->GetPlayer();
auto seat = event->GetSeat();

if (!vehiclePassengers.contains(vehicle))
vehiclePassengers[vehicle] = {};

vehiclePassengers[vehicle][seat] = player;
}
else if (evType == alt::CEvent::Type::PLAYER_LEAVE_VEHICLE)
{
auto event = static_cast<const alt::CPlayerLeaveVehicleEvent*>(ev);
auto vehicle = event->GetTarget();
auto player = event->GetPlayer();

if (vehiclePassengers.contains(vehicle))
{
for ( auto it = vehiclePassengers[vehicle].begin(); it != vehiclePassengers[vehicle].end(); )
{
if (it->second == player)
it = vehiclePassengers[vehicle].erase(it);
else
++it;
}

if (vehiclePassengers[vehicle].empty())
vehiclePassengers.erase(vehicle);
}
}
else if (evType == alt::CEvent::Type::PLAYER_CHANGE_VEHICLE_SEAT)
{
auto event = static_cast<const alt::CPlayerChangeVehicleSeatEvent*>(ev);
auto vehicle = event->GetTarget();

if (!vehiclePassengers.contains(vehicle))
vehiclePassengers[vehicle] = {};

vehiclePassengers[vehicle].erase(event->GetOldSeat());
vehiclePassengers[vehicle][event->GetNewSeat()] = event->GetPlayer();
}
else if (evType == alt::CEvent::Type::REMOVE_BASE_OBJECT_EVENT)
{
auto event = static_cast<const alt::CRemoveBaseObjectEvent*>(ev);

#undef GetObject
auto entity = event->GetObject();

if (entity->GetType() == alt::IBaseObject::Type::VEHICLE)
{
auto vehicle = entity->As<alt::IVehicle>();
if (vehiclePassengers.contains(vehicle))
vehiclePassengers.erase(vehicle);
}
}
}

void CNodeResourceImpl::OnTick()
{
v8::Locker locker(isolate);
Expand Down
1 change: 1 addition & 0 deletions server/src/CNodeResourceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class CNodeResourceImpl : public V8ResourceImpl
bool Stop() override;

void OnEvent(const alt::CEvent* ev) override;
void HandleVehiclePassengerSeatEvents(const alt::CEvent* ev);
void OnTick() override;

bool MakeClient(alt::IResource::CreationInfo* info, std::vector<std::string>) override;
Expand Down
22 changes: 22 additions & 0 deletions server/src/bindings/Vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,27 @@ static void RoofClosedGetter(v8::Local<v8::String> name, const v8::PropertyCallb
V8_RETURN_BOOLEAN(_this->GetRoofState() == 1);
}

static void GetPassengers(v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value>& info)
{
V8_GET_ISOLATE_CONTEXT_RESOURCE();
V8_GET_THIS_BASE_OBJECT(_this, IVehicle);

auto obj = v8::Object::New(isolate);
auto passengers = V8ResourceImpl::vehiclePassengers[_this];

if (!passengers.empty())
{
for (auto& [seat, player] : passengers)
{
auto entity = resource->GetBaseObjectOrNull(player);
if (!entity->IsNull())
obj->Set(ctx, seat, entity);
}
}

V8_RETURN(obj);
}

extern V8Class v8Entity;
extern V8Class v8Vehicle("Vehicle",
v8Entity,
Expand All @@ -244,6 +265,7 @@ extern V8Class v8Vehicle("Vehicle",
// Common getter/setters
V8Helpers::SetAccessor<IVehicle, bool, &IVehicle::IsDestroyed>(isolate, tpl, "destroyed");
V8Helpers::SetAccessor<IVehicle, IPlayer*, &IVehicle::GetDriver>(isolate, tpl, "driver");
V8Helpers::SetAccessor(isolate, tpl, "passengers", &GetPassengers);
V8Helpers::SetAccessor<IVehicle, Vector3f, &IVehicle::GetVelocity>(isolate, tpl, "velocity");
V8Helpers::SetAccessor<IVehicle, Quaternion, &IVehicle::GetQuaternion, &IVehicle::SetQuaternion>(isolate, tpl, "quaternion");

Expand Down
4 changes: 4 additions & 0 deletions shared/IRuntimeEventHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class IRuntimeEventHandler
// All events the module uses, which need to be enabled and never disabled
static constexpr std::array internalEvents = {
EventType::CONNECTION_COMPLETE, EventType::DISCONNECT_EVENT, EventType::GAME_ENTITY_CREATE, EventType::GAME_ENTITY_DESTROY, EventType::RESOURCE_STOP
#ifdef ALT_SERVER_API
// required for vehicle seat stuff
, EventType::PLAYER_ENTER_VEHICLE, EventType::PLAYER_LEAVE_VEHICLE, EventType::PLAYER_CHANGE_VEHICLE_SEAT, EventType::REMOVE_BASE_OBJECT_EVENT
#endif
};

// Keeps track of the event handlers registered for all events
Expand Down
4 changes: 4 additions & 0 deletions shared/V8ResourceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ class V8ResourceImpl : public alt::IResource::Impl
return static_cast<alt::IResource*>(ctx->GetAlignedPointerFromEmbedderData(1));
}

#ifdef ALT_SERVER_API
static inline std::unordered_map<alt::IVehicle*, std::unordered_map<uint8_t, alt::IPlayer*>> vehiclePassengers{};
#endif

protected:
v8::Isolate* isolate;
alt::IResource* resource;
Expand Down

0 comments on commit 72fadbc

Please sign in to comment.