From b18dc7ba14a58c2933a7bf2da7bda8d6262d9f20 Mon Sep 17 00:00:00 2001 From: wsor4035 <24964441+wsor4035@users.noreply.github.com> Date: Sun, 8 Sep 2024 18:01:52 -0400 Subject: [PATCH] add player compatibility api (#41) --- .luacheckrc | 3 ++ README.md | 22 +++++++------- doc/player.md | 13 ++++++++ init.lua | 1 + mod.conf | 2 +- src/player.lua | 8 +++++ src/player/farlands_reloaded.lua | 51 ++++++++++++++++++++++++++++++++ src/player/mineclonia.lua | 40 +++++++++++++++++++++++++ src/player/minetest.lua | 41 +++++++++++++++++++++++++ src/player/xcompat_agnostic.lua | 41 +++++++++++++++++++++++++ 10 files changed, 210 insertions(+), 12 deletions(-) create mode 100644 doc/player.md create mode 100644 src/player.lua create mode 100644 src/player/farlands_reloaded.lua create mode 100644 src/player/mineclonia.lua create mode 100644 src/player/minetest.lua create mode 100644 src/player/xcompat_agnostic.lua diff --git a/.luacheckrc b/.luacheckrc index 7dff721..e46563b 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -15,4 +15,7 @@ read_globals = { "rp_sounds", "mtt", "sounds", + "player_api", + "mcl_player", + "fl_player", } diff --git a/README.md b/README.md index ba388d4..e3d1e26 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # XCompat -[![luacheck](https://github.com/mt-mods/xcompat/workflows/luacheck/badge.svg)](https://github.com/mt-mods/xcompat/actions) +[![luacheck](https://github.com/mt-mods/xcompat/actions/workflows/luacheck.yml/badge.svg?branch=master)](https://github.com/mt-mods/xcompat/actions/workflows/luacheck.yml) [![ContentDB](https://content.minetest.net/packages/mt-mods/xcompat/shields/downloads/)](https://content.minetest.net/packages/mt-mods/xcompat/) Provides cross compatibility between games and mods for sounds and crafting materials. @@ -15,16 +15,16 @@ See the respective sub apis doc file in /doc for detailed documentation. ## Directly supported games and mods -| Games | Sounds | Materials | Textures | -| ----------------- | --------- | --------- | --------- | -| Minetest Game | x | x | x | -| MineClone2 | x | x | | -| Mineclonia | x | x | | -| Hades Revisited | x | x | | -| Farlands Reloaded | x | x | x | -| Exile | x | | | -| KSurvive 2 | x | | | -| Forgotten Lands | x | | | +| Games | Sounds | Materials | Textures | Player | +| ----------------- | --------- | --------- | --------- | ------ | +| Minetest Game | x | x | x | x | +| MineClone2 | x | x | | x | +| Mineclonia | x | x | | x | +| Hades Revisited | x | x | | | +| Farlands Reloaded | x | x | x | x | +| Exile | x | | | | +| KSurvive 2 | x | | | | +| Forgotten Lands | x | | | | For functions see /doc/functions.md for the specifics relating to the function diff --git a/doc/player.md b/doc/player.md new file mode 100644 index 0000000..cdb8431 --- /dev/null +++ b/doc/player.md @@ -0,0 +1,13 @@ +# Player API + +mimic mtg player_api + +## NOTE + +`xcompat.player.player_attached` + +read/write from it is fine, looping over it is not as it is a proxy table. this +would need lua5.2 __pairs/__ipairs metamethods support which i could polyfill +for using https://stackoverflow.com/a/77354254 but didnt feel like doing at +this time. (luajit supports this via 5.2 extensions). additionally see issue: +https://github.com/minetest/minetest/issues/15133 \ No newline at end of file diff --git a/init.lua b/init.lua index c4c622f..883b0d3 100644 --- a/init.lua +++ b/init.lua @@ -11,6 +11,7 @@ xcompat.sounds = dofile(modpath .. "/src/sounds.lua") xcompat.materials = dofile(modpath .. "/src/materials.lua") xcompat.textures = dofile(modpath .. "/src/textures.lua") xcompat.functions = dofile(modpath .. "/src/functions.lua") +xcompat.player = dofile(modpath .. "/src/player.lua") local function validate_sound(key) if key and xcompat.sounds[key] then diff --git a/mod.conf b/mod.conf index 5e00a53..cdecf52 100644 --- a/mod.conf +++ b/mod.conf @@ -1,3 +1,3 @@ name = xcompat description = Provides cross compatibility between mods and games for sounds and crafting materials. -optional_depends = default, fl_stone, fl_trees, mcl_sounds, hades_sounds, ks_sounds, nodes_nature, fl_topsoil, fl_trees, mcl_core, farming, x_farming, sounds, mtt +optional_depends = default, fl_stone, fl_trees, mcl_sounds, hades_sounds, ks_sounds, nodes_nature, fl_topsoil, fl_trees, mcl_core, farming, x_farming, sounds, mtt, player_api, mcl_player, fl_player diff --git a/src/player.lua b/src/player.lua new file mode 100644 index 0000000..fa03f41 --- /dev/null +++ b/src/player.lua @@ -0,0 +1,8 @@ +local filename = xcompat.gameid + +--if we dont have a player file for the game, use minetest +if not xcompat.utilities.file_exists(xcompat.modpath .. "/src/player/" .. filename .. ".lua") then + filename = "xcompat_agnostic" +end + +return dofile(xcompat.modpath .. "/src/player/" .. filename .. ".lua") \ No newline at end of file diff --git a/src/player/farlands_reloaded.lua b/src/player/farlands_reloaded.lua new file mode 100644 index 0000000..288d9ba --- /dev/null +++ b/src/player/farlands_reloaded.lua @@ -0,0 +1,51 @@ +local papi = {} + +local models = {} +function papi.register_model(name, def) + models[name] = def +end + +function papi.set_model(player, model_name) + local model = models[model_name] + + if not model then return end + + player:set_properties({ + mesh = model_name, + textures = model.textures, + visual = "mesh", + visual_size = model.visual_size, + stepheight = model.stepheight + }) +end + +function papi.get_animation(_) + --stub to keep from crashing +end + +function papi.get_textures(player) + return player:get_properties().textures +end + +function papi.set_textures(player, textures) + player:set_properties({textures = textures}) +end + +function papi.set_animation(player, anim_name, speed, loop) + player:set_animation(fl_player.animations[anim_name], speed, 0, loop) +end + +local metatable = { + __index = function (_, key) + return fl_player.ignore[key] + end, + __newindex = function (_, key, value) + rawset(fl_player.ignore, key, value) + end +} + +papi.player_attached = {} + +setmetatable(papi.player_attached, metatable) + +return papi \ No newline at end of file diff --git a/src/player/mineclonia.lua b/src/player/mineclonia.lua new file mode 100644 index 0000000..4758be9 --- /dev/null +++ b/src/player/mineclonia.lua @@ -0,0 +1,40 @@ +local papi = {} + +function papi.register_model(name, def) + return mcl_player.player_register_model(name, def) +end + +function papi.set_model(player, model) + return mcl_player.player_set_model(player, model) +end + +function papi.get_animation(player) + return mcl_player.player_get_animation(player) +end + +function papi.get_textures(player) + return player:get_properties().textures +end + +function papi.set_textures(player, textures) + player:set_properties({textures = textures}) +end + +function papi.set_animation(player, anim_name, speed, _) + return mcl_player.player_set_animation(player, anim_name, speed) +end + +local metatable = { + __index = function (_, key) + return mcl_player.player_attached[key] + end, + __newindex = function (_, key, value) + rawset(mcl_player.player_attached, key, value) + end +} + +papi.player_attached = {} + +setmetatable(papi.player_attached, metatable) + +return papi \ No newline at end of file diff --git a/src/player/minetest.lua b/src/player/minetest.lua new file mode 100644 index 0000000..75f4f08 --- /dev/null +++ b/src/player/minetest.lua @@ -0,0 +1,41 @@ +local papi = {} + +function papi.register_model(name, def) + return player_api.register_model(name, def) +end + +function papi.set_model(player, model) + return player_api.set_model(player, model) +end + +function papi.get_animation(player) + return player_api.get_animation(player) +end + +function papi.get_textures(player) + return player_api.get_textures(player) +end + +function papi.set_textures(player, texture) + return player_api.set_textures(player, texture) +end + +function papi.set_animation(player, anim_name, speed, loop) + return player_api.set_animation(player, anim_name, speed, loop) +end + + +local metatable = { + __index = function (_, key) + return player_api.player_attached[key] + end, + __newindex = function (_, key, value) + rawset(player_api.player_attached, key, value) + end +} + +papi.player_attached = {} + +setmetatable(papi.player_attached, metatable) + +return papi \ No newline at end of file diff --git a/src/player/xcompat_agnostic.lua b/src/player/xcompat_agnostic.lua new file mode 100644 index 0000000..3a5523e --- /dev/null +++ b/src/player/xcompat_agnostic.lua @@ -0,0 +1,41 @@ +local papi = {} + +local models = {} +function papi.register_model(name, def) + models[name] = def +end + +function papi.set_model(player, model_name) + local model = models[model_name] + + if not model then return end + + player:set_properties({ + mesh = model_name, + textures = model.textures, + visual = "mesh", + visual_size = model.visual_size, + stepheight = model.stepheight + }) +end + +function papi.get_animation(_) + --stub to keep from crashing +end + +function papi.get_textures(player) + return player:get_properties().textures +end + +function papi.set_textures(player, textures) + player:set_properties({textures = textures}) +end + +function papi.set_animation(_, _, _, _) + --stub to keep from crashing +end + +--nothing to do here as we have no globalstep .....that we know about anyways +papi.player_attached = {} + +return papi \ No newline at end of file