From 66e604cec76cf1ba7f3bfcf57f3eb06f09222ad1 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Tue, 21 Jun 2016 16:15:25 +0300 Subject: [PATCH] Add back some of the entity search functions. --- source/Base/Algo.cpp | 27 +++++++++++++++++++++++++++ source/Entity/Player.cpp | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/source/Base/Algo.cpp b/source/Base/Algo.cpp index 3d595feb..5d2513cf 100644 --- a/source/Base/Algo.cpp +++ b/source/Base/Algo.cpp @@ -14,6 +14,32 @@ namespace SqMod { namespace Algo { +// ------------------------------------------------------------------------------------------------ +static const Object & Blip_FindBySprID(Int32 sprid) +{ + // Perform a range check on the specified identifier + if (sprid < 0) + { + STHROWF("The specified sprite identifier is invalid: %d", sprid); + } + // Obtain the ends of the entity pool + Core::Blips::const_iterator itr = Core::Get().GetBlips().cbegin(); + Core::Blips::const_iterator end = Core::Get().GetBlips().cend(); + // Process each entity in the pool + for (; itr != end; ++itr) + { + // Does the identifier match the specified one? + if (itr->mSprID == sprid) + { + return itr->mObj; // Stop searching and return this entity + } + } + // Unable to locate a blip matching the specified identifier + return NullObject(); +} + +// ------------------------------------------------------------------------------------------------ + // ================================================================================================ void Register(HSQUIRRELVM vm) { @@ -25,6 +51,7 @@ void Register(HSQUIRRELVM vm) .Func(_SC("TagBegins"), &Entity< CBlip >::FirstWhereTagBegins) .Func(_SC("TagEnds"), &Entity< CBlip >::FirstWhereTagEnds) .Func(_SC("TagContains"), &Entity< CBlip >::FirstWhereTagContains) + .Func(_SC("WithSprID"), &Blip_FindBySprID) ); fns.Bind(_SC("Checkpoint"), Table(vm) diff --git a/source/Entity/Player.cpp b/source/Entity/Player.cpp index 55999adf..4e886ca8 100644 --- a/source/Entity/Player.cpp +++ b/source/Entity/Player.cpp @@ -2061,6 +2061,37 @@ SQInteger CPlayer::AnnounceEx(HSQUIRRELVM vm) return 0; } +// ------------------------------------------------------------------------------------------------ +static const Object & Player_FindAuto(Object & by) +{ + 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 NullObject(); +} + // ================================================================================================ void Register_CPlayer(HSQUIRRELVM vm) { @@ -2220,6 +2251,8 @@ void Register_CPlayer(HSQUIRRELVM vm) (_SC("SetAnimation"), &CPlayer::SetAnimation) .Overload< void (CPlayer::*)(Int32, Int32) const > (_SC("SetAnimation"), &CPlayer::SetAnimation) + // Static Functions + .StaticFunc(_SC("Find"), &Player_FindAuto) // Raw Squirrel Methods .SquirrelFunc(_SC("Msg"), &CPlayer::Msg) .SquirrelFunc(_SC("MsgP"), &CPlayer::MsgP)