diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index 482e9f187..4501d6cc9 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -124,6 +124,7 @@ set(libdevilutionx_SRCS lua/modules/monsters.cpp lua/modules/player.cpp lua/modules/render.cpp + lua/modules/system.cpp lua/modules/towners.cpp lua/repl.cpp diff --git a/Source/lua/lua_global.cpp b/Source/lua/lua_global.cpp index 3453d1ac1..4fc69075e 100644 --- a/Source/lua/lua_global.cpp +++ b/Source/lua/lua_global.cpp @@ -21,6 +21,7 @@ #include "lua/modules/monsters.hpp" #include "lua/modules/player.hpp" #include "lua/modules/render.hpp" +#include "lua/modules/system.hpp" #include "lua/modules/towners.hpp" #include "monster.h" #include "options.h" @@ -286,6 +287,7 @@ void LuaInitialize() "devilutionx.render", LuaRenderModule(lua), "devilutionx.towners", LuaTownersModule(lua), "devilutionx.hellfire", LuaHellfireModule(lua), + "devilutionx.system", LuaSystemModule(lua), "devilutionx.floatingnumbers", LuaFloatingNumbersModule(lua), "devilutionx.message", [](std::string_view text) { EventPlrMsg(text, UiFlags::ColorRed); }, // This package is loaded without a sandbox: diff --git a/Source/lua/modules/floatingnumbers.cpp b/Source/lua/modules/floatingnumbers.cpp index d4caa233b..922b0f9f5 100644 --- a/Source/lua/modules/floatingnumbers.cpp +++ b/Source/lua/modules/floatingnumbers.cpp @@ -2,13 +2,6 @@ #include -#ifdef USE_SDL3 -#include -#else -#include -#endif - -#include "DiabloUI/ui_flags.hpp" #include "engine/point.hpp" #include "lua/metadoc.hpp" #include "qol/floatingnumbers.h" @@ -25,26 +18,6 @@ sol::table LuaFloatingNumbersModule(sol::state_view &lua) AddFloatingNumber(pos, { 0, 0 }, text, style, id.value_or(0), reverseDirection.value_or(false)); }); - LuaSetDocFn(table, "get_ticks", "() -> integer", "Returns the number of milliseconds since the game started.", - []() { return static_cast(SDL_GetTicks()); }); - - auto flags = lua.create_table(); - flags["DarkRed"] = UiFlags::ColorUiSilver; - flags["Yellow"] = UiFlags::ColorYellow; - flags["Gold"] = UiFlags::ColorGold; - flags["Black"] = UiFlags::ColorBlack; - flags["White"] = UiFlags::ColorWhite; - flags["WhiteGold"] = UiFlags::ColorWhitegold; - flags["Red"] = UiFlags::ColorRed; - flags["Blue"] = UiFlags::ColorBlue; - flags["Orange"] = UiFlags::ColorOrange; - - flags["Small"] = UiFlags::FontSize12; - flags["Medium"] = UiFlags::FontSize24; - flags["Large"] = UiFlags::FontSize30; - - table["Flags"] = flags; - return table; } diff --git a/Source/lua/modules/render.cpp b/Source/lua/modules/render.cpp index 99bd550fa..7c1c5147a 100644 --- a/Source/lua/modules/render.cpp +++ b/Source/lua/modules/render.cpp @@ -2,6 +2,7 @@ #include +#include "DiabloUI/ui_flags.hpp" #include "engine/dx.h" #include "engine/render/text_render.hpp" #include "lua/metadoc.hpp" @@ -19,6 +20,24 @@ sol::table LuaRenderModule(sol::state_view &lua) "Returns the screen width", []() { return gnScreenWidth; }); LuaSetDocFn(table, "screen_height", "()", "Returns the screen height", []() { return gnScreenHeight; }); + + auto uiFlags = lua.create_table(); + uiFlags["DarkRed"] = UiFlags::ColorUiSilver; + uiFlags["Yellow"] = UiFlags::ColorYellow; + uiFlags["Gold"] = UiFlags::ColorGold; + uiFlags["Black"] = UiFlags::ColorBlack; + uiFlags["White"] = UiFlags::ColorWhite; + uiFlags["WhiteGold"] = UiFlags::ColorWhitegold; + uiFlags["Red"] = UiFlags::ColorRed; + uiFlags["Blue"] = UiFlags::ColorBlue; + uiFlags["Orange"] = UiFlags::ColorOrange; + + uiFlags["Small"] = UiFlags::FontSize12; + uiFlags["Medium"] = UiFlags::FontSize24; + uiFlags["Large"] = UiFlags::FontSize30; + + table["UiFlags"] = uiFlags; + return table; } diff --git a/Source/lua/modules/system.cpp b/Source/lua/modules/system.cpp new file mode 100644 index 000000000..0ef6a09d0 --- /dev/null +++ b/Source/lua/modules/system.cpp @@ -0,0 +1,25 @@ +#include "lua/modules/system.hpp" + +#include + +#ifdef USE_SDL3 +#include +#else +#include +#endif + +#include "lua/metadoc.hpp" + +namespace devilution { + +sol::table LuaSystemModule(sol::state_view &lua) +{ + sol::table table = lua.create_table(); + + LuaSetDocFn(table, "get_ticks", "() -> integer", "Returns the number of milliseconds since the game started.", + []() { return static_cast(SDL_GetTicks()); }); + + return table; +} + +} // namespace devilution diff --git a/Source/lua/modules/system.hpp b/Source/lua/modules/system.hpp new file mode 100644 index 000000000..fa2629a7c --- /dev/null +++ b/Source/lua/modules/system.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include + +namespace devilution { + +sol::table LuaSystemModule(sol::state_view &lua); + +} // namespace devilution diff --git a/assets/lua/mods/Floating Numbers - Damage/init.lua b/assets/lua/mods/Floating Numbers - Damage/init.lua index fe02b3f13..c30f8c63f 100644 --- a/assets/lua/mods/Floating Numbers - Damage/init.lua +++ b/assets/lua/mods/Floating Numbers - Damage/init.lua @@ -1,6 +1,8 @@ local floatingnumbers = require("devilutionx.floatingnumbers") local events = require("devilutionx.events") local player = require("devilutionx.player") +local system = require("devilutionx.system") +local render = require("devilutionx.render") local DAMAGE_TYPE = { PHYSICAL = 0, @@ -15,19 +17,19 @@ local function get_damage_style(damage_val, damage_type) local v = damage_val if v >= 64 * 300 then - style = style | floatingnumbers.Flags.Large + style = style | render.UiFlags.Large elseif v >= 64 * 100 then - style = style | floatingnumbers.Flags.Medium + style = style | render.UiFlags.Medium else - style = style | floatingnumbers.Flags.Small + style = style | render.UiFlags.Small end local damage_type_styles = { - [DAMAGE_TYPE.PHYSICAL] = floatingnumbers.Flags.Gold, - [DAMAGE_TYPE.FIRE] = floatingnumbers.Flags.DarkRed, - [DAMAGE_TYPE.LIGHTNING] = floatingnumbers.Flags.Blue, - [DAMAGE_TYPE.MAGIC] = floatingnumbers.Flags.Orange, - [DAMAGE_TYPE.ACID] = floatingnumbers.Flags.Yellow, + [DAMAGE_TYPE.PHYSICAL] = render.UiFlags.Gold, + [DAMAGE_TYPE.FIRE] = render.UiFlags.DarkRed, + [DAMAGE_TYPE.LIGHTNING] = render.UiFlags.Blue, + [DAMAGE_TYPE.MAGIC] = render.UiFlags.Orange, + [DAMAGE_TYPE.ACID] = render.UiFlags.Yellow, } local type_style = damage_type_styles[damage_type] @@ -51,7 +53,7 @@ local MERGE_WINDOW_MS = 100 events.OnMonsterTakeDamage.add(function(monster, damage, damage_type) local id = monster.id - local now = floatingnumbers.get_ticks() + local now = system.get_ticks() local entry = accumulated_damage[id] if entry and (now - entry.time) < MERGE_WINDOW_MS then @@ -70,7 +72,7 @@ end) events.OnPlayerTakeDamage.add(function(_player, damage, damage_type) if _player == player.self() then local id = _player.id - local now = floatingnumbers.get_ticks() + local now = system.get_ticks() local entry = accumulated_damage[id] if entry and (now - entry.time) < MERGE_WINDOW_MS then diff --git a/assets/lua/mods/Floating Numbers - XP/init.lua b/assets/lua/mods/Floating Numbers - XP/init.lua index b9777ef92..dd88682eb 100644 --- a/assets/lua/mods/Floating Numbers - XP/init.lua +++ b/assets/lua/mods/Floating Numbers - XP/init.lua @@ -1,14 +1,16 @@ local floatingnumbers = require("devilutionx.floatingnumbers") local events = require("devilutionx.events") local player = require("devilutionx.player") +local system = require("devilutionx.system") +local render = require("devilutionx.render") local accumulated_xp = {} -local MERGE_WINDOW_MS = 100 +local MERGE_WINDOW_MS = 100 events.OnPlayerGainExperience.add(function(_player, experience) - if _player == player.self() then + if _player == player.self() then local id = _player.id - local now = floatingnumbers.get_ticks() + local now = system.get_ticks() local entry = accumulated_xp[id] if entry and (now - entry.time) < MERGE_WINDOW_MS then @@ -20,6 +22,6 @@ events.OnPlayerGainExperience.add(function(_player, experience) entry.time = now local text = tostring(entry.experience) .. " XP" - floatingnumbers.add(text, _player.position, floatingnumbers.Flags.White, id, true) + floatingnumbers.add(text, _player.position, render.UiFlags.White, id, true) end end)