diff --git a/src/Components/Modules/Bots.cpp b/src/Components/Modules/Bots.cpp index 21261246..f81d1190 100644 --- a/src/Components/Modules/Bots.cpp +++ b/src/Components/Modules/Bots.cpp @@ -127,8 +127,8 @@ namespace Components return; } - const auto* gentity = Script::GetEntity(entref); - auto* client = Script::GetClient(gentity); + const auto* ent = Game::GetPlayerEntity(entref); + auto* client = Script::GetClient(ent); if (!client->bIsTestClient) { @@ -141,7 +141,7 @@ namespace Components Script::AddMethod("IsTestClient", [](Game::scr_entref_t entref) // Usage: IsTestClient(); { - const auto* gentity = Script::GetEntity(entref); + const auto* gentity = Game::GetPlayerEntity(entref); const auto* client = Script::GetClient(gentity); Game::Scr_AddBool(client->bIsTestClient == 1); @@ -149,8 +149,8 @@ namespace Components Script::AddMethod("BotStop", [](Game::scr_entref_t entref) // Usage: BotStop(); { - const auto* gentity = Script::GetEntity(entref); - const auto* client = Script::GetClient(gentity); + const auto* ent = Game::GetPlayerEntity(entref); + const auto* client = Script::GetClient(ent); if (!client->bIsTestClient) { @@ -167,8 +167,8 @@ namespace Components { const auto* weapon = Game::Scr_GetString(0); - const auto* gentity = Script::GetEntity(entref); - const auto* client = Script::GetClient(gentity); + const auto* ent = Game::GetPlayerEntity(entref); + const auto* client = Script::GetClient(ent); if (!client->bIsTestClient) { @@ -197,8 +197,8 @@ namespace Components return; } - const auto* gentity = Script::GetEntity(entref); - const auto* client = Script::GetClient(gentity); + const auto* ent = Game::GetPlayerEntity(entref); + const auto* client = Script::GetClient(ent); if (!client->bIsTestClient) { @@ -234,8 +234,8 @@ namespace Components auto forwardInt = Game::Scr_GetInt(0); auto rightInt = Game::Scr_GetInt(1); - const auto* gentity = Script::GetEntity(entref); - const auto* client = Script::GetClient(gentity); + const auto* ent = Game::GetPlayerEntity(entref); + const auto* client = Script::GetClient(ent); if (!client->bIsTestClient) { diff --git a/src/Components/Modules/ClientCommand.cpp b/src/Components/Modules/ClientCommand.cpp index 5b7136c6..f39845ce 100644 --- a/src/Components/Modules/ClientCommand.cpp +++ b/src/Components/Modules/ClientCommand.cpp @@ -177,13 +177,7 @@ namespace Components { Script::AddMethod("Noclip", [](Game::scr_entref_t entref) // gsc: Noclip(); { - const auto* ent = Script::GetEntity(entref); - - if (ent->client == nullptr) - { - Game::Scr_ObjectError(Utils::String::VA("^1NoClip: entity %i is not a client\n", ent->s.number)); - return; - } + const auto* ent = Game::GetPlayerEntity(entref); if (Game::Scr_GetNumParam() >= 1u) { @@ -204,13 +198,7 @@ namespace Components Script::AddMethod("Ufo", [](Game::scr_entref_t entref) // gsc: Ufo(); { - const auto* ent = Script::GetEntity(entref); - - if (ent->client == nullptr) - { - Game::Scr_ObjectError(Utils::String::VA("^1Ufo: entity %i is not a client\n", ent->s.number)); - return; - } + const auto* ent = Game::GetPlayerEntity(entref); if (Game::Scr_GetNumParam() >= 1u) { @@ -231,7 +219,7 @@ namespace Components Script::AddMethod("God", [](Game::scr_entref_t entref) // gsc: God(); { - auto* ent = Script::GetEntity(entref); + auto* ent = Game::GetEntity(entref); if (Game::Scr_GetNumParam() >= 1u) { @@ -252,7 +240,7 @@ namespace Components Script::AddMethod("Demigod", [](Game::scr_entref_t entref) // gsc: Demigod(); { - auto* ent = Script::GetEntity(entref); + auto* ent = Game::GetEntity(entref); if (Game::Scr_GetNumParam() >= 1u) { @@ -273,7 +261,7 @@ namespace Components Script::AddMethod("Notarget", [](Game::scr_entref_t entref) // gsc: Notarget(); { - auto* ent = Script::GetEntity(entref); + auto* ent = Game::GetEntity(entref); if (Game::Scr_GetNumParam() >= 1u) { diff --git a/src/Components/Modules/Script.cpp b/src/Components/Modules/Script.cpp index 184d26c0..23aae4a2 100644 --- a/src/Components/Modules/Script.cpp +++ b/src/Components/Modules/Script.cpp @@ -534,34 +534,23 @@ namespace Components } } - Game::gentity_t* Script::GetEntity(const Game::scr_entref_t entref) + Game::client_t* Script::GetClient(const Game::gentity_t* ent) { - if (entref.classnum != 0) + assert(ent != nullptr); + + if (ent->client == nullptr) { - Game::Scr_ObjectError("Not an entity"); + Game::Scr_ObjectError(Utils::String::VA("Entity %i is not a player", ent->s.number)); return nullptr; } - assert(entref.entnum < Game::MAX_GENTITIES); - - return &Game::g_entities[entref.entnum]; - } - - Game::client_t* Script::GetClient(const Game::gentity_t* gentity) - { - if (gentity->client == nullptr) + if (ent->s.number >= *Game::svs_numclients) { - Game::Scr_ObjectError(Utils::String::VA("Entity %i is not a player", gentity->s.number)); + Game::Scr_ObjectError(Utils::String::VA("Entity %i is out of bounds", ent->s.number)); return nullptr; } - if (gentity->s.number >= *Game::svs_numclients) - { - Game::Scr_ObjectError(Utils::String::VA("Entity %i is out of bounds", gentity->s.number)); - return nullptr; - } - - return &Game::svs_clients[gentity->s.number]; + return &Game::svs_clients[ent->s.number]; } void Script::AddFunctions() @@ -703,13 +692,7 @@ namespace Components // PlayerCmd_AreControlsFrozen GSC function from Black Ops 2 Script::AddMethod("AreControlsFrozen", [](Game::scr_entref_t entref) // Usage: self AreControlsFrozen(); { - const auto* ent = Script::GetEntity(entref); - - if (ent->client == nullptr) - { - Game::Scr_ObjectError(Utils::String::VA("Entity %i is not a player", ent->s.number)); - return; - } + const auto* ent = Game::GetPlayerEntity(entref); Game::Scr_AddBool((ent->client->flags & Game::PLAYER_FLAG_FROZEN) != 0); }); diff --git a/src/Components/Modules/Script.hpp b/src/Components/Modules/Script.hpp index 1d1144aa..731ffcb7 100644 --- a/src/Components/Modules/Script.hpp +++ b/src/Components/Modules/Script.hpp @@ -16,7 +16,6 @@ namespace Components static void OnVMShutdown(Utils::Slot callback); - static Game::gentity_t* GetEntity(const Game::scr_entref_t entref); static Game::client_t* GetClient(const Game::gentity_t* gentity); private: diff --git a/src/Components/Modules/ScriptExtension.cpp b/src/Components/Modules/ScriptExtension.cpp index a6823ea8..80e9cefd 100644 --- a/src/Components/Modules/ScriptExtension.cpp +++ b/src/Components/Modules/ScriptExtension.cpp @@ -132,8 +132,8 @@ namespace Components // ScriptExtension methods Script::AddMethod("GetIp", [](Game::scr_entref_t entref) // gsc: self GetIp() { - const auto* gentity = Script::GetEntity(entref); - const auto* client = Script::GetClient(gentity); + const auto* ent = Game::GetPlayerEntity(entref); + const auto* client = Script::GetClient(ent); std::string ip = Game::NET_AdrToString(client->netchan.remoteAddress); @@ -147,8 +147,8 @@ namespace Components Script::AddMethod("GetPing", [](Game::scr_entref_t entref) // gsc: self GetPing() { - const auto* gentity = Script::GetEntity(entref); - const auto* client = Script::GetClient(gentity); + const auto* ent = Game::GetPlayerEntity(entref); + const auto* client = Script::GetClient(ent); Game::Scr_AddInt(client->ping); }); diff --git a/src/Game/Functions.cpp b/src/Game/Functions.cpp index 9f57c7a7..a611fda8 100644 --- a/src/Game/Functions.cpp +++ b/src/Game/Functions.cpp @@ -276,13 +276,18 @@ namespace Game Scr_AddObject_t Scr_AddObject = Scr_AddObject_t(0x430F40); Scr_Notify_t Scr_Notify = Scr_Notify_t(0x4A4750); Scr_NotifyLevel_t Scr_NotifyLevel = Scr_NotifyLevel_t(0x4D9C30); + Scr_Error_t Scr_Error = Scr_Error_t(0x61E8B0); Scr_ObjectError_t Scr_ObjectError = Scr_ObjectError_t(0x42EF40); Scr_ParamError_t Scr_ParamError = Scr_ParamError_t(0x4FBC70); + Scr_GetType_t Scr_GetType = Scr_GetType_t(0x422900); Scr_ClearOutParams_t Scr_ClearOutParams = Scr_ClearOutParams_t(0x4386E0); + GetEntity_t GetEntity = GetEntity_t(0x4BC270); + GetPlayerEntity_t GetPlayerEntity = GetPlayerEntity_t(0x49C4A0); + Scr_RegisterFunction_t Scr_RegisterFunction = Scr_RegisterFunction_t(0x492D50); Scr_ShutdownAllocNode_t Scr_ShutdownAllocNode = Scr_ShutdownAllocNode_t(0x441650); Scr_IsSystemActive_t Scr_IsSystemActive = Scr_IsSystemActive_t(0x4B24E0); diff --git a/src/Game/Functions.hpp b/src/Game/Functions.hpp index 31926ec4..e7720ee9 100644 --- a/src/Game/Functions.hpp +++ b/src/Game/Functions.hpp @@ -717,6 +717,12 @@ namespace Game typedef void(__cdecl * Scr_ParamError_t)(unsigned int paramIndex, const char*); extern Scr_ParamError_t Scr_ParamError; + typedef gentity_s*(__cdecl * GetPlayerEntity_t)(scr_entref_t entref); + extern GetPlayerEntity_t GetPlayerEntity; + + typedef gentity_s*(__cdecl * GetEntity_t)(scr_entref_t entref); + extern GetEntity_t GetEntity; + typedef script_t* (__cdecl * Script_Alloc_t)(int length); extern Script_Alloc_t Script_Alloc;