From 93966dc3b1acb2237391efacc75a9a5bcdbe10b3 Mon Sep 17 00:00:00 2001 From: Quentin Date: Fri, 28 Jul 2023 10:07:17 +0200 Subject: [PATCH] Feat lua stats (#1875) --- docs/lua/tables/stats.md | 299 +++++++++++++++++++++++++++++++++++++ src/lua/bindings/stats.cpp | 293 ++++++++++++++++++++++++++++++++++++ src/lua/bindings/stats.hpp | 7 + src/lua/lua_module.cpp | 2 + 4 files changed, 601 insertions(+) create mode 100644 docs/lua/tables/stats.md create mode 100644 src/lua/bindings/stats.cpp create mode 100644 src/lua/bindings/stats.hpp diff --git a/docs/lua/tables/stats.md b/docs/lua/tables/stats.md new file mode 100644 index 00000000..af374f12 --- /dev/null +++ b/docs/lua/tables/stats.md @@ -0,0 +1,299 @@ +# Table: stats + +Table for manipulating GTA stats. + +## Functions (21) + +### `get_character_index()` + +- **Returns:** + - `integer`: The current multiplayer character index (0 or 1). + +**Example Usage:** +```lua +integer = stats.get_character_index() +``` + +### `get_bool(stat_hash)` + +- **Parameters:** + - `stat_hash` (integer): the stat hash. + +- **Returns:** + - `boolean`: The value of the given stat. + +**Example Usage:** +```lua +boolean = stats.get_bool(stat_hash) +``` + +### `get_bool(stat_name)` + +- **Parameters:** + - `stat_name` (string): the stat name. + +- **Returns:** + - `boolean`: The value of the given stat. + +**Example Usage:** +```lua +boolean = stats.get_bool(stat_name) +``` + +### `get_bool_masked(stat_hash, bit_index)` + +- **Parameters:** + - `stat_hash` (integer): the stat hash. + - `bit_index` (integer): bit index. + +- **Returns:** + - `boolean`: The value of the given stat. + +**Example Usage:** +```lua +boolean = stats.get_bool_masked(stat_hash, bit_index) +``` + +### `get_bool_masked(stat_name, bit_index)` + +- **Parameters:** + - `stat_name` (string): the stat name. + - `bit_index` (integer): bit index. + +- **Returns:** + - `boolean`: The value of the given stat. + +**Example Usage:** +```lua +boolean = stats.get_bool_masked(stat_name, bit_index) +``` + +### `get_float(stat_hash)` + +- **Parameters:** + - `stat_hash` (integer): the stat hash. + +- **Returns:** + - `float`: The value of the given stat. + +**Example Usage:** +```lua +float = stats.get_float(stat_hash) +``` + +### `get_float(stat_name)` + +- **Parameters:** + - `stat_name` (string): the stat name. + +- **Returns:** + - `float`: The value of the given stat. + +**Example Usage:** +```lua +float = stats.get_float(stat_name) +``` + +### `get_int(stat_hash)` + +- **Parameters:** + - `stat_hash` (integer): the stat hash. + +- **Returns:** + - `integer`: The value of the given stat. + +**Example Usage:** +```lua +integer = stats.get_int(stat_hash) +``` + +### `get_int(stat_name)` + +- **Parameters:** + - `stat_name` (string): the stat name. + +- **Returns:** + - `integer`: The value of the given stat. + +**Example Usage:** +```lua +integer = stats.get_int(stat_name) +``` + +### `get_masked_int(stat_hash, bit_start, bit_size)` + +- **Parameters:** + - `stat_hash` (integer): the stat hash. + - `bit_start` (integer): bit start. + - `bit_size` (integer): bit size. + +- **Returns:** + - `integer`: The value of the given stat. + +**Example Usage:** +```lua +integer = stats.get_masked_int(stat_hash, bit_start, bit_size) +``` + +### `get_masked_int(stat_name, bit_index, bit_size)` + +- **Parameters:** + - `stat_name` (string): the stat name. + - `bit_index` (integer): bit index. + - `bit_size` (integer): bit size. + +- **Returns:** + - `integer`: The value of the given stat. + +**Example Usage:** +```lua +integer = stats.get_masked_int(stat_name, bit_index, bit_size) +``` + +### `set_bool(stat_hash, new_value)` + +- **Parameters:** + - `stat_hash` (integer): the stat hash. + - `new_value` (boolean): the new value for the stat. + +- **Returns:** + - `boolean`: True if succeeded. + +**Example Usage:** +```lua +boolean = stats.set_bool(stat_hash, new_value) +``` + +### `set_bool(stat_name, new_value)` + +- **Parameters:** + - `stat_name` (string): the stat name. + - `new_value` (boolean): the new value for the stat. + +- **Returns:** + - `boolean`: True if succeeded. + +**Example Usage:** +```lua +boolean = stats.set_bool(stat_name, new_value) +``` + +### `set_bool_masked(stat_hash, new_value, bit_index)` + +- **Parameters:** + - `stat_hash` (integer): the stat hash. + - `new_value` (boolean): the new value for the stat. + - `bit_index` (integer): bit_index. + +- **Returns:** + - `boolean`: True if succeeded. + +**Example Usage:** +```lua +boolean = stats.set_bool_masked(stat_hash, new_value, bit_index) +``` + +### `set_bool_masked(stat_name, new_value, bit_index)` + +- **Parameters:** + - `stat_name` (string): the stat name. + - `new_value` (boolean): the new value for the stat. + - `bit_index` (integer): bit_index. + +- **Returns:** + - `boolean`: True if succeeded. + +**Example Usage:** +```lua +boolean = stats.set_bool_masked(stat_name, new_value, bit_index) +``` + +### `set_float(stat_hash, new_value)` + +- **Parameters:** + - `stat_hash` (integer): the stat hash. + - `new_value` (float): the new value for the stat. + +- **Returns:** + - `boolean`: True if succeeded. + +**Example Usage:** +```lua +boolean = stats.set_float(stat_hash, new_value) +``` + +### `set_float(stat_name, new_value)` + +- **Parameters:** + - `stat_name` (string): the stat name. + - `new_value` (float): the new value for the stat. + +- **Returns:** + - `boolean`: True if succeeded. + +**Example Usage:** +```lua +boolean = stats.set_float(stat_name, new_value) +``` + +### `set_int(stat_hash, new_value)` + +- **Parameters:** + - `stat_hash` (integer): the stat hash. + - `new_value` (integer): the new value for the stat. + +- **Returns:** + - `boolean`: True if succeeded. + +**Example Usage:** +```lua +boolean = stats.set_int(stat_hash, new_value) +``` + +### `set_int(stat_name, new_value)` + +- **Parameters:** + - `stat_name` (string): the stat name. + - `new_value` (integer): the new value for the stat. + +- **Returns:** + - `boolean`: True if succeeded. + +**Example Usage:** +```lua +boolean = stats.set_int(stat_name, new_value) +``` + +### `set_masked_int(stat_hash, new_value, bit_start, bit_size)` + +- **Parameters:** + - `stat_hash` (integer): the stat hash. + - `new_value` (integer): the new value for the stat. + - `bit_start` (integer): bit_start. + - `bit_size` (integer): bit_size. + +- **Returns:** + - `boolean`: True if succeeded. + +**Example Usage:** +```lua +boolean = stats.set_masked_int(stat_hash, new_value, bit_start, bit_size) +``` + +### `set_masked_int(stat_name, new_value, bit_start, bit_size)` + +- **Parameters:** + - `stat_name` (string): the stat name. + - `new_value` (integer): the new value for the stat. + - `bit_start` (integer): bit_start. + - `bit_size` (integer): bit_size. + +- **Returns:** + - `boolean`: True if succeeded. + +**Example Usage:** +```lua +boolean = stats.set_masked_int(stat_name, new_value, bit_start, bit_size) +``` + + diff --git a/src/lua/bindings/stats.cpp b/src/lua/bindings/stats.cpp new file mode 100644 index 00000000..2adfc36b --- /dev/null +++ b/src/lua/bindings/stats.cpp @@ -0,0 +1,293 @@ +#pragma once +#include "stats.hpp" + +#include "natives.hpp" + +namespace lua::stats +{ + // Lua API: Table + // Name: stats + // Table for manipulating GTA stats. + + // Lua API: Function + // Table: stats + // Name: get_character_index + // Returns: integer: The current multiplayer character index (0 or 1). + static int get_character_index() + { + int character_index = 0; + STATS::STAT_GET_INT(RAGE_JOAAT("MPPLY_LAST_MP_CHAR"), &character_index, -1); + return character_index; + } + + static Hash stat_text_to_hash(std::string& text) + { + if (text[0] == '$') + { + text = text.substr(1); + } + + std::transform(text.begin(), text.end(), text.begin(), ::toupper); + + if (text.substr(0, 3) == "MPX") + text[2] = get_character_index() + '0'; + + return rage::joaat(text); + } + + // Lua API: Function + // Table: stats + // Name: get_bool + // Param: stat_hash: integer: the stat hash. + // Returns: boolean: The value of the given stat. + static bool get_bool_hash(Hash stat_hash) + { + int out_value; + STATS::STAT_GET_BOOL(stat_hash, &out_value, -1); + + return out_value; + } + + // Lua API: Function + // Table: stats + // Name: get_bool + // Param: stat_name: string: the stat name. + // Returns: boolean: The value of the given stat. + static bool get_bool_name(std::string stat_name) + { + return get_bool_hash(stat_text_to_hash(stat_name)); + } + + // Lua API: Function + // Table: stats + // Name: get_bool_masked + // Param: stat_hash: integer: the stat hash. + // Param: bit_index: integer: bit index. + // Returns: boolean: The value of the given stat. + static bool get_bool_masked_hash(Hash stat_hash, int bit_index) + { + int out_value; + constexpr int bit_count = 8; + STATS::STAT_GET_MASKED_INT(stat_hash, &out_value, bit_index, bit_count, -1); + + return out_value; + } + + // Lua API: Function + // Table: stats + // Name: get_bool_masked + // Param: stat_name: string: the stat name. + // Param: bit_index: integer: bit index. + // Returns: boolean: The value of the given stat. + static bool get_bool_masked_name(std::string stat_name, int bit_index) + { + return get_bool_masked_hash(stat_text_to_hash(stat_name), bit_index); + } + + // Lua API: Function + // Table: stats + // Name: get_float + // Param: stat_hash: integer: the stat hash. + // Returns: float: The value of the given stat. + static float get_float_hash(Hash stat_hash) + { + float out_value; + STATS::STAT_GET_FLOAT(stat_hash, &out_value, -1); + + return out_value; + } + + // Lua API: Function + // Table: stats + // Name: get_float + // Param: stat_name: string: the stat name. + // Returns: float: The value of the given stat. + static float get_float_name(std::string stat_name) + { + return get_float_hash(stat_text_to_hash(stat_name)); + } + + // Lua API: Function + // Table: stats + // Name: get_int + // Param: stat_hash: integer: the stat hash. + // Returns: integer: The value of the given stat. + static int get_int_hash(Hash stat_hash) + { + int out_value; + STATS::STAT_GET_INT(stat_hash, &out_value, -1); + + return out_value; + } + + // Lua API: Function + // Table: stats + // Name: get_int + // Param: stat_name: string: the stat name. + // Returns: integer: The value of the given stat. + static int get_int_name(std::string stat_name) + { + return get_int_hash(stat_text_to_hash(stat_name)); + } + + // Lua API: Function + // Table: stats + // Name: get_masked_int + // Param: stat_hash: integer: the stat hash. + // Param: bit_start: integer: bit start. + // Param: bit_size: integer: bit size. + // Returns: integer: The value of the given stat. + static int get_masked_int_hash(Hash stat_hash, int bit_index, int bit_size) + { + int out_value; + STATS::STAT_GET_MASKED_INT(stat_hash, &out_value, bit_index, bit_size, -1); + + return out_value; + } + + // Lua API: Function + // Table: stats + // Name: get_masked_int + // Param: stat_name: string: the stat name. + // Param: bit_index: integer: bit index. + // Param: bit_size: integer: bit size. + // Returns: integer: The value of the given stat. + static int get_masked_int_name(std::string stat_name, int bit_index, int bit_size) + { + return get_masked_int_hash(stat_text_to_hash(stat_name), bit_index, bit_size); + } + + // Lua API: Function + // Table: stats + // Name: set_bool + // Param: stat_hash: integer: the stat hash. + // Param: new_value: boolean: the new value for the stat. + // Returns: boolean: True if succeeded. + static bool set_bool_hash(Hash stat_hash, bool new_value) + { + return STATS::STAT_SET_BOOL(stat_hash, new_value, true); + } + + // Lua API: Function + // Table: stats + // Name: set_bool + // Param: stat_name: string: the stat name. + // Param: new_value: boolean: the new value for the stat. + // Returns: boolean: True if succeeded. + static bool set_bool_name(std::string stat_name, bool new_value) + { + return set_bool_hash(stat_text_to_hash(stat_name), new_value); + } + + // Lua API: Function + // Table: stats + // Name: set_bool_masked + // Param: stat_hash: integer: the stat hash. + // Param: new_value: boolean: the new value for the stat. + // Param: bit_index: integer: bit_index. + // Returns: boolean: True if succeeded. + static bool set_bool_masked_hash(Hash stat_hash, bool new_value, int bit_index) + { + constexpr int bit_count = 8; + return STATS::STAT_SET_MASKED_INT(stat_hash, new_value, bit_index, bit_count, true); + } + + // Lua API: Function + // Table: stats + // Name: set_bool_masked + // Param: stat_name: string: the stat name. + // Param: new_value: boolean: the new value for the stat. + // Param: bit_index: integer: bit_index. + // Returns: boolean: True if succeeded. + static bool set_bool_masked_name(std::string stat_name, bool new_value, int bit_index) + { + return set_bool_masked_hash(stat_text_to_hash(stat_name), new_value, bit_index); + } + + // Lua API: Function + // Table: stats + // Name: set_float + // Param: stat_hash: integer: the stat hash. + // Param: new_value: float: the new value for the stat. + // Returns: boolean: True if succeeded. + static bool set_float_hash(Hash stat_hash, float new_value) + { + return STATS::STAT_SET_FLOAT(stat_hash, new_value, true); + } + + // Lua API: Function + // Table: stats + // Name: set_float + // Param: stat_name: string: the stat name. + // Param: new_value: float: the new value for the stat. + // Returns: boolean: True if succeeded. + static bool set_float_name(std::string stat_name, float new_value) + { + return set_float_hash(stat_text_to_hash(stat_name), new_value); + } + + // Lua API: Function + // Table: stats + // Name: set_int + // Param: stat_hash: integer: the stat hash. + // Param: new_value: integer: the new value for the stat. + // Returns: boolean: True if succeeded. + static bool set_int_hash(Hash stat_hash, int new_value) + { + return STATS::STAT_SET_INT(stat_hash, new_value, true); + } + + // Lua API: Function + // Table: stats + // Name: set_int + // Param: stat_name: string: the stat name. + // Param: new_value: integer: the new value for the stat. + // Returns: boolean: True if succeeded. + static bool set_int_name(std::string stat_name, int new_value) + { + return set_int_hash(stat_text_to_hash(stat_name), new_value); + } + + // Lua API: Function + // Table: stats + // Name: set_masked_int + // Param: stat_hash: integer: the stat hash. + // Param: new_value: integer: the new value for the stat. + // Param: bit_start: integer: bit_start. + // Param: bit_size: integer: bit_size. + // Returns: boolean: True if succeeded. + static bool set_masked_int_hash(Hash stat_hash, int new_value, int bit_start, int bit_size) + { + return STATS::STAT_SET_MASKED_INT(stat_hash, new_value, bit_start, bit_size, true); + } + + // Lua API: Function + // Table: stats + // Name: set_masked_int + // Param: stat_name: string: the stat name. + // Param: new_value: integer: the new value for the stat. + // Param: bit_start: integer: bit_start. + // Param: bit_size: integer: bit_size. + // Returns: boolean: True if succeeded. + static bool set_masked_int_name(std::string stat_name, int new_value, int bit_start, int bit_size) + { + return set_masked_int_hash(stat_text_to_hash(stat_name), new_value, bit_start, bit_size); + } + + void bind(sol::state& state) + { + auto ns = state["stats"].get_or_create(); + + ns["get_bool"] = sol::overload(get_bool_hash, get_bool_name); + ns["get_bool_masked"] = sol::overload(get_bool_masked_hash, get_bool_masked_name); + ns["get_float"] = sol::overload(get_float_hash, get_float_name); + ns["get_int"] = sol::overload(get_int_hash, get_int_name); + ns["get_masked_int"] = sol::overload(get_masked_int_hash, get_masked_int_name); + + ns["set_bool"] = sol::overload(set_bool_hash, set_bool_name); + ns["set_bool_masked"] = sol::overload(set_bool_masked_hash, set_bool_masked_name); + ns["set_float"] = sol::overload(set_float_hash, set_float_name); + ns["set_int"] = sol::overload(set_int_hash, set_int_name); + ns["set_masked_int"] = sol::overload(set_masked_int_hash, set_masked_int_name); + } +} \ No newline at end of file diff --git a/src/lua/bindings/stats.hpp b/src/lua/bindings/stats.hpp new file mode 100644 index 00000000..a63ee093 --- /dev/null +++ b/src/lua/bindings/stats.hpp @@ -0,0 +1,7 @@ +#pragma once +#include "lua/sol.hpp" + +namespace lua::stats +{ + void bind(sol::state& state); +} \ No newline at end of file diff --git a/src/lua/lua_module.cpp b/src/lua/lua_module.cpp index c61df5fd..5b1da583 100644 --- a/src/lua/lua_module.cpp +++ b/src/lua/lua_module.cpp @@ -13,6 +13,7 @@ #include "bindings/native.hpp" #include "bindings/network.hpp" #include "bindings/script.hpp" +#include "bindings/stats.hpp" #include "bindings/tunables.hpp" #include "bindings/vector.hpp" #include "file_manager.hpp" @@ -208,6 +209,7 @@ namespace big lua::global_table::bind(m_state); lua::imgui::bind(m_state, m_state.globals()); lua::entities::bind(m_state); + lua::stats::bind(m_state); } void lua_module::load_and_call_script()