1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 08:47:17 +01:00

Return null entity instances instead of null for the functions that need to return an entity instance.

Finally fix the player unspectating bug and introduce a new event for when a player spectates none.
Update the global functions that check for player existance to use the new implementation.
This commit is contained in:
Sandu Liviu Catalin 2017-08-06 18:14:58 +03:00
parent 8e07d37ec2
commit 8125400f7f
10 changed files with 36 additions and 38 deletions

View File

@ -131,7 +131,7 @@ static const LightObj & Blip_FindBySprID(Int32 sprid)
} }
} }
// Unable to locate a blip matching the specified identifier // Unable to locate a blip matching the specified identifier
return NullLightObj(); return Core::Get().GetNullBlip();
} }
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------

View File

@ -143,6 +143,7 @@ static const EnumElement g_EventEnum[] = {
{_SC("PlayerKeyPress"), EVT_PLAYERKEYPRESS}, {_SC("PlayerKeyPress"), EVT_PLAYERKEYPRESS},
{_SC("PlayerKeyRelease"), EVT_PLAYERKEYRELEASE}, {_SC("PlayerKeyRelease"), EVT_PLAYERKEYRELEASE},
{_SC("PlayerSpectate"), EVT_PLAYERSPECTATE}, {_SC("PlayerSpectate"), EVT_PLAYERSPECTATE},
{_SC("PlayerUnspectate"), EVT_PLAYERUNSPECTATE},
{_SC("PlayerCrashReport"), EVT_PLAYERCRASHREPORT}, {_SC("PlayerCrashReport"), EVT_PLAYERCRASHREPORT},
{_SC("VehicleExplode"), EVT_VEHICLEEXPLODE}, {_SC("VehicleExplode"), EVT_VEHICLEEXPLODE},
{_SC("VehicleRespawn"), EVT_VEHICLERESPAWN}, {_SC("VehicleRespawn"), EVT_VEHICLERESPAWN},

View File

@ -530,6 +530,7 @@ protected:
SignalPair mOnKeyPress; SignalPair mOnKeyPress;
SignalPair mOnKeyRelease; SignalPair mOnKeyRelease;
SignalPair mOnSpectate; SignalPair mOnSpectate;
SignalPair mOnUnspectate;
SignalPair mOnCrashreport; SignalPair mOnCrashreport;
SignalPair mOnObjectShot; SignalPair mOnObjectShot;
SignalPair mOnObjectTouched; SignalPair mOnObjectTouched;
@ -1159,6 +1160,7 @@ public:
void EmitPlayerKeyPress(Int32 player_id, Int32 bind_id); void EmitPlayerKeyPress(Int32 player_id, Int32 bind_id);
void EmitPlayerKeyRelease(Int32 player_id, Int32 bind_id); void EmitPlayerKeyRelease(Int32 player_id, Int32 bind_id);
void EmitPlayerSpectate(Int32 player_id, Int32 target_player_id); void EmitPlayerSpectate(Int32 player_id, Int32 target_player_id);
void EmitPlayerUnspectate(Int32 player_id);
void EmitPlayerCrashreport(Int32 player_id, CCStr report); void EmitPlayerCrashreport(Int32 player_id, CCStr report);
void EmitVehicleExplode(Int32 vehicle_id); void EmitVehicleExplode(Int32 vehicle_id);
void EmitVehicleRespawn(Int32 vehicle_id); void EmitVehicleRespawn(Int32 vehicle_id);
@ -1316,6 +1318,7 @@ public:
SignalPair mOnPlayerKeyPress; SignalPair mOnPlayerKeyPress;
SignalPair mOnPlayerKeyRelease; SignalPair mOnPlayerKeyRelease;
SignalPair mOnPlayerSpectate; SignalPair mOnPlayerSpectate;
SignalPair mOnPlayerUnspectate;
SignalPair mOnPlayerCrashreport; SignalPair mOnPlayerCrashreport;
SignalPair mOnVehicleExplode; SignalPair mOnVehicleExplode;
SignalPair mOnVehicleRespawn; SignalPair mOnVehicleRespawn;

View File

@ -594,6 +594,14 @@ void Core::EmitPlayerSpectate(Int32 player_id, Int32 target_player_id)
(*mOnPlayerSpectate.first)(_player.mObj, _target.mObj); (*mOnPlayerSpectate.first)(_player.mObj, _target.mObj);
} }
// ------------------------------------------------------------------------------------------------
void Core::EmitPlayerUnspectate(Int32 player_id)
{
PlayerInst & _player = m_Players.at(player_id);
(*_player.mOnUnspectate.first)();
(*mOnPlayerUnspectate.first)(_player.mObj);
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Core::EmitPlayerCrashreport(Int32 player_id, CCStr report) void Core::EmitPlayerCrashreport(Int32 player_id, CCStr report)
{ {

View File

@ -620,6 +620,7 @@ void Core::PlayerInst::InitEvents()
InitSignalPair(mOnKeyPress, mEvents, "KeyPress"); InitSignalPair(mOnKeyPress, mEvents, "KeyPress");
InitSignalPair(mOnKeyRelease, mEvents, "KeyRelease"); InitSignalPair(mOnKeyRelease, mEvents, "KeyRelease");
InitSignalPair(mOnSpectate, mEvents, "Spectate"); InitSignalPair(mOnSpectate, mEvents, "Spectate");
InitSignalPair(mOnUnspectate, mEvents, "Unspectate");
InitSignalPair(mOnCrashreport, mEvents, "Crashreport"); InitSignalPair(mOnCrashreport, mEvents, "Crashreport");
InitSignalPair(mOnObjectShot, mEvents, "ObjectShot"); InitSignalPair(mOnObjectShot, mEvents, "ObjectShot");
InitSignalPair(mOnObjectTouched, mEvents, "ObjectTouched"); InitSignalPair(mOnObjectTouched, mEvents, "ObjectTouched");
@ -698,6 +699,7 @@ void Core::PlayerInst::DropEvents()
ResetSignalPair(mOnKeyPress); ResetSignalPair(mOnKeyPress);
ResetSignalPair(mOnKeyRelease); ResetSignalPair(mOnKeyRelease);
ResetSignalPair(mOnSpectate); ResetSignalPair(mOnSpectate);
ResetSignalPair(mOnUnspectate);
ResetSignalPair(mOnCrashreport); ResetSignalPair(mOnCrashreport);
ResetSignalPair(mOnObjectShot); ResetSignalPair(mOnObjectShot);
ResetSignalPair(mOnObjectTouched); ResetSignalPair(mOnObjectTouched);

View File

@ -127,6 +127,7 @@ void Core::InitEvents()
InitSignalPair(mOnPlayerKeyPress, m_Events, "PlayerKeyPress"); InitSignalPair(mOnPlayerKeyPress, m_Events, "PlayerKeyPress");
InitSignalPair(mOnPlayerKeyRelease, m_Events, "PlayerKeyRelease"); InitSignalPair(mOnPlayerKeyRelease, m_Events, "PlayerKeyRelease");
InitSignalPair(mOnPlayerSpectate, m_Events, "PlayerSpectate"); InitSignalPair(mOnPlayerSpectate, m_Events, "PlayerSpectate");
InitSignalPair(mOnPlayerUnspectate, m_Events, "PlayerUnspectate");
InitSignalPair(mOnPlayerCrashreport, m_Events, "PlayerCrashreport"); InitSignalPair(mOnPlayerCrashreport, m_Events, "PlayerCrashreport");
InitSignalPair(mOnVehicleExplode, m_Events, "VehicleExplode"); InitSignalPair(mOnVehicleExplode, m_Events, "VehicleExplode");
InitSignalPair(mOnVehicleRespawn, m_Events, "VehicleRespawn"); InitSignalPair(mOnVehicleRespawn, m_Events, "VehicleRespawn");
@ -259,6 +260,7 @@ void Core::DropEvents()
ResetSignalPair(mOnPlayerKeyPress); ResetSignalPair(mOnPlayerKeyPress);
ResetSignalPair(mOnPlayerKeyRelease); ResetSignalPair(mOnPlayerKeyRelease);
ResetSignalPair(mOnPlayerSpectate); ResetSignalPair(mOnPlayerSpectate);
ResetSignalPair(mOnPlayerUnspectate);
ResetSignalPair(mOnPlayerCrashreport); ResetSignalPair(mOnPlayerCrashreport);
ResetSignalPair(mOnVehicleExplode); ResetSignalPair(mOnVehicleExplode);
ResetSignalPair(mOnVehicleRespawn); ResetSignalPair(mOnVehicleRespawn);

View File

@ -1098,7 +1098,7 @@ LightObj & CPlayer::GetVehicle() const
return Core::Get().GetVehicle(id).mObj; return Core::Get().GetVehicle(id).mObj;
} }
// Default to a null object // Default to a null object
return NullLightObj(); return Core::Get().GetNullVehicle();
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -1299,7 +1299,7 @@ LightObj & CPlayer::StandingOnVehicle() const
return Core::Get().GetVehicle(id).mObj; return Core::Get().GetVehicle(id).mObj;
} }
// Default to a null object // Default to a null object
return NullLightObj(); return Core::Get().GetNullVehicle();
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -1316,7 +1316,7 @@ LightObj & CPlayer::StandingOnObject() const
return Core::Get().GetObject(id).mObj; return Core::Get().GetObject(id).mObj;
} }
// Default to a null object // Default to a null object
return NullLightObj(); return Core::Get().GetNullObject();
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -1342,7 +1342,7 @@ LightObj & CPlayer::GetSpectator() const
return Core::Get().GetPlayer(id).mObj; return Core::Get().GetPlayer(id).mObj;
} }
// Default to a null object // Default to a null object
return NullLightObj(); return Core::Get().GetNullPlayer();
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -2405,7 +2405,7 @@ SQInteger CPlayer::AnnounceEx(HSQUIRRELVM vm)
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
static SQInteger Player_FindAuto(HSQUIRRELVM vm) SQInteger Player_FindAuto(HSQUIRRELVM vm)
{ {
// See if a search token was specified // See if a search token was specified
if (sq_gettop(vm) <= 1) if (sq_gettop(vm) <= 1)
@ -2493,7 +2493,7 @@ static SQInteger Player_FindAuto(HSQUIRRELVM vm)
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
static SQInteger Player_ExistsAuto(HSQUIRRELVM vm) SQInteger Player_ExistsAuto(HSQUIRRELVM vm)
{ {
// See if a search token was specified // See if a search token was specified
if (sq_gettop(vm) <= 1) if (sq_gettop(vm) <= 1)

View File

@ -676,7 +676,14 @@ static void OnPlayerSpectate(int32_t player_id, int32_t target_player_id)
// Attempt to forward the event // Attempt to forward the event
try try
{ {
Core::Get().EmitPlayerSpectate(player_id, target_player_id); if (target_player_id < 0)
{
Core::Get().EmitPlayerUnspectate(player_id);
}
else
{
Core::Get().EmitPlayerSpectate(player_id, target_player_id);
}
} }
SQMOD_CATCH_EVENT_EXCEPTION(OnPlayerSpectate) SQMOD_CATCH_EVENT_EXCEPTION(OnPlayerSpectate)
// See if a reload was requested // See if a reload was requested

View File

@ -18,35 +18,8 @@
namespace SqMod { namespace SqMod {
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
static const LightObj & FindPlayer(Object & by) extern SQInteger Player_FindAuto(HSQUIRRELVM vm);
{ extern SQInteger Player_ExistsAuto(HSQUIRRELVM vm);
switch (by.GetType())
{
case OT_INTEGER:
{
return Core::Get().GetPlayer(by.Cast< Int32 >()).mObj;
} break;
case OT_FLOAT:
{
return Core::Get().GetPlayer(std::round(by.Cast< Float32 >())).mObj;
} break;
case OT_STRING:
{
// Obtain the argument as a string
String str(by.Cast< String >());
// Attempt to locate the player with this name
Int32 id = _Func->GetPlayerIdFromName(&str[0]);
// Was there a player with this name?
if (VALID_ENTITYEX(id, SQMOD_PLAYER_POOL))
{
Core::Get().GetPlayer(id).mObj;
}
} break;
default: STHROWF("Unsupported search identifier");
}
// Default to a null object
return NullLightObj();
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
extern void Register_Broadcast(HSQUIRRELVM vm); extern void Register_Broadcast(HSQUIRRELVM vm);
@ -150,7 +123,8 @@ void Register_Misc(HSQUIRRELVM vm)
RootTable(vm).Bind(_SC("SqServer"), srvns); RootTable(vm).Bind(_SC("SqServer"), srvns);
RootTable(vm) RootTable(vm)
.Func(_SC("FindPlayer"), &FindPlayer) .SquirrelFunc(_SC("FindPlayer"), &Player_FindAuto)
.SquirrelFunc(_SC("PlayerExists"), &Player_ExistsAuto)
.Func(_SC("GetKeyCodeName"), &GetKeyCodeName) .Func(_SC("GetKeyCodeName"), &GetKeyCodeName)
.FmtFunc(_SC("SetKeyCodeName"), &SetKeyCodeName) .FmtFunc(_SC("SetKeyCodeName"), &SetKeyCodeName)
.Func(_SC("GetModelName"), &GetModelName) .Func(_SC("GetModelName"), &GetModelName)

View File

@ -364,6 +364,7 @@ enum EventType
EVT_PLAYERKEYPRESS, EVT_PLAYERKEYPRESS,
EVT_PLAYERKEYRELEASE, EVT_PLAYERKEYRELEASE,
EVT_PLAYERSPECTATE, EVT_PLAYERSPECTATE,
EVT_PLAYERUNSPECTATE,
EVT_PLAYERCRASHREPORT, EVT_PLAYERCRASHREPORT,
EVT_VEHICLEEXPLODE, EVT_VEHICLEEXPLODE,
EVT_VEHICLERESPAWN, EVT_VEHICLERESPAWN,