1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-02-21 20:27:13 +01:00

Implement an unrestricted pool of custom weapon names and fall back to it when looking for weapon names.

This commit is contained in:
Sandu Liviu Catalin 2018-04-13 20:31:18 +03:00
parent 999cec8d9d
commit d668242901
3 changed files with 65 additions and 4 deletions

View File

@ -141,6 +141,8 @@ void Register_Misc(HSQUIRRELVM vm)
.Func(_SC("IsAutomobileValid"), &IsAutomobileValid) .Func(_SC("IsAutomobileValid"), &IsAutomobileValid)
.Func(_SC("GetWeaponName"), &GetWeaponName) .Func(_SC("GetWeaponName"), &GetWeaponName)
.FmtFunc(_SC("SetWeaponName"), &SetWeaponName) .FmtFunc(_SC("SetWeaponName"), &SetWeaponName)
.Func(_SC("GetCustomWeaponNamePoolSize"), &GetCustomWeaponNamePoolSize)
.Func(_SC("ClearCustomWeaponNamePool"), &ClearCustomWeaponNamePool)
.FmtFunc(_SC("GetWeaponID"), &GetWeaponID) .FmtFunc(_SC("GetWeaponID"), &GetWeaponID)
.Func(_SC("IsWeaponValid"), &IsWeaponValid) .Func(_SC("IsWeaponValid"), &IsWeaponValid)
.Func(_SC("WeaponToModel"), &WeaponToModel) .Func(_SC("WeaponToModel"), &WeaponToModel)

View File

@ -5,6 +5,7 @@
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include <cstring> #include <cstring>
#include <algorithm> #include <algorithm>
#include <unordered_map>
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
namespace SqMod { namespace SqMod {
@ -48,20 +49,63 @@ static String CS_Weapon_Names[] = {
/* 68 */ "", /* 69 */ "", /* 68 */ "", /* 69 */ "",
/* 70 */ "Suicide", /* 71 */ "" /* 70 */ "Suicide", /* 71 */ ""
}; };
/// Fall back for custom weapon names.
static std::unordered_map<Uint32, String> CS_Custom_Weapon_Names{};
// ------------------------------------------------------------------------------------------------
static inline bool IsCustomWeapon(Uint32 id)
{
return (id > 70);
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
CCStr GetWeaponName(Uint32 id) CCStr GetWeaponName(Uint32 id)
{ {
return (id > 70) ? _SC("") : CS_Weapon_Names[id].c_str(); // Can we consider this a custom weapon ID?
if (IsCustomWeapon(id))
{
// Attempt to look for the specified identifier
auto it = CS_Custom_Weapon_Names.find(id);
// If we found anything than return the associated name
if ((it != CS_Custom_Weapon_Names.end()))
{
return it->second.c_str();
}
}
else
{
return CS_Weapon_Names[id].c_str(); // Use the standard weapon name
}
// Fall back to an empty name
return _SC("");
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void SetWeaponName(Uint32 id, const StackStrF & name) void SetWeaponName(Uint32 id, const StackStrF & name)
{ {
if (id <= 70) // Can we consider this a custom weapon ID?
{ if (IsCustomWeapon(id))
{
// Attempt to insert or update the name into the custom weapon table
CS_Custom_Weapon_Names[id] = String(name.mPtr, name.mLen);
}
else
{
// Attempt to insert or update the name into the standard weapon table
CS_Weapon_Names[id].assign(name.mPtr); CS_Weapon_Names[id].assign(name.mPtr);
} }
}
// ------------------------------------------------------------------------------------------------
Uint32 GetCustomWeaponNamePoolSize()
{
return static_cast< Uint32 >(CS_Custom_Weapon_Names.size());
}
// ------------------------------------------------------------------------------------------------
void ClearCustomWeaponNamePool()
{
CS_Custom_Weapon_Names.clear();
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------

View File

@ -17,6 +17,21 @@ CSStr GetWeaponName(Uint32 id);
*/ */
void SetWeaponName(Uint32 id, const StackStrF & name); void SetWeaponName(Uint32 id, const StackStrF & name);
/* ------------------------------------------------------------------------------------------------
* Retrieve the total number of identifiers in the pool of custom weapon names.
*/
Uint32 GetCustomWeaponNamePoolSize();
/* ------------------------------------------------------------------------------------------------
* Clear all identifiersand associated names from the pool of custom weapon names.
*/
void ClearCustomWeaponNamePool();
/* ------------------------------------------------------------------------------------------------
* Modify the name associated with a weapon identifier.
*/
void SetWeaponName(Uint32 id, const StackStrF & name);
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* Convert a weapon name to a weapon identifier. * Convert a weapon name to a weapon identifier.
*/ */