Browse Source

Extract lua functions from floatingnumbers to system and render modules (#8416)

pull/8417/head
Trihedraf 2 months ago committed by GitHub
parent
commit
b540d0911e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 1
      Source/CMakeLists.txt
  2. 2
      Source/lua/lua_global.cpp
  3. 27
      Source/lua/modules/floatingnumbers.cpp
  4. 19
      Source/lua/modules/render.cpp
  5. 25
      Source/lua/modules/system.cpp
  6. 9
      Source/lua/modules/system.hpp
  7. 22
      assets/lua/mods/Floating Numbers - Damage/init.lua
  8. 10
      assets/lua/mods/Floating Numbers - XP/init.lua

1
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

2
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:

27
Source/lua/modules/floatingnumbers.cpp

@ -2,13 +2,6 @@
#include <sol/sol.hpp>
#ifdef USE_SDL3
#include <SDL3/SDL_timer.h>
#else
#include <SDL.h>
#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<int>(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;
}

19
Source/lua/modules/render.cpp

@ -2,6 +2,7 @@
#include <sol/sol.hpp>
#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;
}

25
Source/lua/modules/system.cpp

@ -0,0 +1,25 @@
#include "lua/modules/system.hpp"
#include <sol/sol.hpp>
#ifdef USE_SDL3
#include <SDL3/SDL_timer.h>
#else
#include <SDL.h>
#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<int>(SDL_GetTicks()); });
return table;
}
} // namespace devilution

9
Source/lua/modules/system.hpp

@ -0,0 +1,9 @@
#pragma once
#include <sol/sol.hpp>
namespace devilution {
sol::table LuaSystemModule(sol::state_view &lua);
} // namespace devilution

22
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

10
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)

Loading…
Cancel
Save