From 1292bdbbcec45613c95aff9f2ea88aa49af25011 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sat, 11 Apr 2020 00:09:11 +0200 Subject: Various features and fixes --- clientmods/preview/init.lua | 40 +++---------------- doc/client_lua_api.txt | 31 ++++++++++++--- src/script/lua_api/l_client.cpp | 26 +++++-------- src/script/lua_api/l_localplayer.cpp | 74 ++++++++++++++++++++++++++++++------ src/script/lua_api/l_localplayer.h | 21 +++++++++- 5 files changed, 123 insertions(+), 69 deletions(-) diff --git a/clientmods/preview/init.lua b/clientmods/preview/init.lua index 5777adcaf..d2440369a 100644 --- a/clientmods/preview/init.lua +++ b/clientmods/preview/init.lua @@ -31,6 +31,7 @@ core.after(4, function() end) core.after(1, function() + print("armor: " .. dump(core.localplayer:get_armor_groups())) id = core.localplayer:hud_add({ hud_elem_type = "text", name = "example", @@ -125,19 +126,6 @@ core.register_chatcommand("dump", { end, }) -core.register_chatcommand("colorize_test", { - func = function(param) - return true, core.colorize("red", param) - end, -}) - -core.register_chatcommand("test_node", { - func = function(param) - core.display_chat_message(dump(core.get_node({x=0, y=0, z=0}))) - core.display_chat_message(dump(core.get_node_or_nil({x=0, y=0, z=0}))) - end, -}) - local function preview_minimap() local minimap = core.ui.minimap if not minimap then @@ -157,7 +145,7 @@ end core.after(2, function() print("[PREVIEW] loaded " .. modname .. " mod") modstorage:set_string("current_mod", modname) - print(modstorage:get_string("current_mod")) + assert(modstorage:get_string("current_mod") == modname) preview_minimap() end) @@ -184,30 +172,12 @@ end) core.register_on_punchnode(function(pos, node) print("The local player punched a node!") - local itemstack = core.get_wielded_item() - --[[ - -- getters - print(dump(itemstack:is_empty())) - print(dump(itemstack:get_name())) - print(dump(itemstack:get_count())) - print(dump(itemstack:get_wear())) - print(dump(itemstack:get_meta())) - print(dump(itemstack:get_metadata() - print(dump(itemstack:is_known())) - --print(dump(itemstack:get_definition())) - print(dump(itemstack:get_tool_capabilities())) - print(dump(itemstack:to_string())) - print(dump(itemstack:to_table())) - -- setters - print(dump(itemstack:set_name("default:dirt"))) - print(dump(itemstack:set_count("95"))) - print(dump(itemstack:set_wear(934))) - print(dump(itemstack:get_meta())) - print(dump(itemstack:get_metadata())) - --]] + local itemstack = core.localplayer:get_wielded_item() print(dump(itemstack:to_table())) print("pos:" .. dump(pos)) print("node:" .. dump(node)) + local meta = core.get_meta(pos) + print("punched meta: " .. (meta and dump(meta:to_table()) or "(missing)")) return false end) diff --git a/doc/client_lua_api.txt b/doc/client_lua_api.txt index 2c8ffd4d4..71df91c68 100644 --- a/doc/client_lua_api.txt +++ b/doc/client_lua_api.txt @@ -804,8 +804,6 @@ Call these functions only at load time! * get max available level for leveled node ### Player -* `minetest.get_wielded_item()` - * Returns the itemstack the local player is holding * `minetest.send_chat_message(message)` * Act as if `message` was typed by the player into the terminal. * `minetest.run_server_chatcommand(cmd, param)` @@ -1006,6 +1004,10 @@ Methods: * returns player HP * `get_name()` * returns player name +* `get_wield_index()` + * returns the index of the wielded item +* `get_wielded_item()` + * returns the itemstack the player is holding * `is_attached()` * returns true if player is attached * `is_touching_ground()` @@ -1029,7 +1031,8 @@ Methods: jump = float, gravity = float, sneak = boolean, - sneak_glitch = boolean + sneak_glitch = boolean, + new_move = boolean, } ``` @@ -1081,8 +1084,26 @@ Methods: * returns last look horizontal angle * `get_last_look_vertical()`: * returns last look vertical angle -* `get_key_pressed()`: - * returns last key typed by the player +* `get_control()`: + * returns pressed player controls + +```lua + { + up = boolean, + down = boolean, + left = boolean, + right = boolean, + jump = boolean, + aux1 = boolean, + sneak = boolean, + zoom = boolean, + LMB = boolean, + RMB = boolean, + } +``` + +* `get_armor_groups()` + * returns a table with the armor group ratings * `hud_add(definition)` * add a HUD element described by HUD def, returns ID number on success and `nil` on failure. * See [`HUD definition`](#hud-definition-hud_add-hud_get) diff --git a/src/script/lua_api/l_client.cpp b/src/script/lua_api/l_client.cpp index e30c05260..aaced7cd0 100644 --- a/src/script/lua_api/l_client.cpp +++ b/src/script/lua_api/l_client.cpp @@ -29,7 +29,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "cpp_api/s_base.h" #include "gettext.h" #include "l_internal.h" -#include "lua_api/l_item.h" #include "lua_api/l_nodemeta.h" #include "gui/mainmenumanager.h" #include "map.h" @@ -245,25 +244,18 @@ int ModApiClient::l_get_language(lua_State *L) return 2; } -// get_wielded_item() -int ModApiClient::l_get_wielded_item(lua_State *L) -{ - Client *client = getClient(L); - LocalPlayer *player = client->getEnv().getLocalPlayer(); - if (!player) - return 0; - - ItemStack selected_item; - player->getWieldedItem(&selected_item, nullptr); - LuaItemStack::create(L, selected_item); - return 1; -} - // get_meta(pos) int ModApiClient::l_get_meta(lua_State *L) { v3s16 p = read_v3s16(L, 1); - NodeMetadata *meta = getClient(L)->getEnv().getMap().getNodeMetadata(p); + + // check restrictions first + bool pos_ok; + getClient(L)->CSMGetNode(p, &pos_ok); + if (!pos_ok) + return 0; + + NodeMetadata *meta = getEnv(L)->getMap().getNodeMetadata(p); NodeMetaRef::createClient(L, meta); return 1; } @@ -390,6 +382,7 @@ int ModApiClient::l_get_node_def(lua_State *L) return 1; } +// get_privilege_list() int ModApiClient::l_get_privilege_list(lua_State *L) { const Client *client = getClient(L); @@ -436,7 +429,6 @@ void ModApiClient::Initialize(lua_State *L, int top) API_FCT(send_respawn); API_FCT(gettext); API_FCT(get_node_or_nil); - API_FCT(get_wielded_item); API_FCT(disconnect); API_FCT(get_meta); API_FCT(sound_play); diff --git a/src/script/lua_api/l_localplayer.cpp b/src/script/lua_api/l_localplayer.cpp index 821b1cb66..851ede535 100644 --- a/src/script/lua_api/l_localplayer.cpp +++ b/src/script/lua_api/l_localplayer.cpp @@ -19,10 +19,12 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "l_localplayer.h" #include "l_internal.h" +#include "lua_api/l_item.h" #include "script/common/c_converter.h" #include "client/localplayer.h" #include "hud.h" #include "common/c_content.h" +#include "client/content_cao.h" LuaLocalPlayer::LuaLocalPlayer(LocalPlayer *m) : m_localplayer(m) { @@ -74,6 +76,26 @@ int LuaLocalPlayer::l_get_name(lua_State *L) return 1; } +// get_wield_index(self) +int LuaLocalPlayer::l_get_wield_index(lua_State *L) +{ + LocalPlayer *player = getobject(L, 1); + + lua_pushinteger(L, player->getWieldIndex()); + return 1; +} + +// get_wielded_item(self) +int LuaLocalPlayer::l_get_wielded_item(lua_State *L) +{ + LocalPlayer *player = getobject(L, 1); + + ItemStack selected_item; + player->getWieldedItem(&selected_item, nullptr); + LuaItemStack::create(L, selected_item); + return 1; +} + int LuaLocalPlayer::l_is_attached(lua_State *L) { LocalPlayer *player = getobject(L, 1); @@ -130,6 +152,7 @@ int LuaLocalPlayer::l_swimming_vertical(lua_State *L) return 1; } +// get_physics_override(self) int LuaLocalPlayer::l_get_physics_override(lua_State *L) { LocalPlayer *player = getobject(L, 1); @@ -150,14 +173,9 @@ int LuaLocalPlayer::l_get_physics_override(lua_State *L) lua_pushboolean(L, player->physics_override_sneak_glitch); lua_setfield(L, -2, "sneak_glitch"); - return 1; -} - -int LuaLocalPlayer::l_get_override_pos(lua_State *L) -{ - LocalPlayer *player = getobject(L, 1); + lua_pushboolean(L, player->physics_override_new_move); + lua_setfield(L, -2, "new_move"); - push_v3f(L, player->getPosition()); return 1; } @@ -193,14 +211,33 @@ int LuaLocalPlayer::l_get_last_look_horizontal(lua_State *L) return 1; } -int LuaLocalPlayer::l_get_key_pressed(lua_State *L) +// get_control(self) +int LuaLocalPlayer::l_get_control(lua_State *L) { LocalPlayer *player = getobject(L, 1); + const PlayerControl &c = player->getPlayerControl(); + + auto set = [L] (const char *name, bool value) { + lua_pushboolean(L, value); + lua_setfield(L, -2, name); + }; + + lua_createtable(L, 0, 12); + set("up", c.up); + set("down", c.down); + set("left", c.left); + set("right", c.right); + set("jump", c.jump); + set("aux1", c.aux1); + set("sneak", c.sneak); + set("zoom", c.zoom); + set("LMB", c.LMB); + set("RMB", c.RMB); - lua_pushinteger(L, player->last_keyPressed); return 1; } +// get_breath(self) int LuaLocalPlayer::l_get_breath(lua_State *L) { LocalPlayer *player = getobject(L, 1); @@ -209,6 +246,7 @@ int LuaLocalPlayer::l_get_breath(lua_State *L) return 1; } +// get_pos(self) int LuaLocalPlayer::l_get_pos(lua_State *L) { LocalPlayer *player = getobject(L, 1); @@ -217,6 +255,7 @@ int LuaLocalPlayer::l_get_pos(lua_State *L) return 1; } +// get_movement_acceleration(self) int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L) { LocalPlayer *player = getobject(L, 1); @@ -234,6 +273,7 @@ int LuaLocalPlayer::l_get_movement_acceleration(lua_State *L) return 1; } +// get_movement_speed(self) int LuaLocalPlayer::l_get_movement_speed(lua_State *L) { LocalPlayer *player = getobject(L, 1); @@ -257,6 +297,7 @@ int LuaLocalPlayer::l_get_movement_speed(lua_State *L) return 1; } +// get_movement(self) int LuaLocalPlayer::l_get_movement(lua_State *L) { LocalPlayer *player = getobject(L, 1); @@ -278,6 +319,13 @@ int LuaLocalPlayer::l_get_movement(lua_State *L) return 1; } +// get_armor_groups(self) +int LuaLocalPlayer::l_get_armor_groups(lua_State *L) +{ + LocalPlayer *player = getobject(L, 1); + push_groups(L, player->getCAO()->getGroups()); + return 1; +} // hud_add(self, form) int LuaLocalPlayer::l_hud_add(lua_State *L) @@ -407,6 +455,8 @@ const luaL_Reg LuaLocalPlayer::methods[] = { luamethod(LuaLocalPlayer, get_velocity), luamethod(LuaLocalPlayer, get_hp), luamethod(LuaLocalPlayer, get_name), + luamethod(LuaLocalPlayer, get_wield_index), + luamethod(LuaLocalPlayer, get_wielded_item), luamethod(LuaLocalPlayer, is_attached), luamethod(LuaLocalPlayer, is_touching_ground), luamethod(LuaLocalPlayer, is_in_liquid), @@ -415,17 +465,19 @@ const luaL_Reg LuaLocalPlayer::methods[] = { luamethod(LuaLocalPlayer, is_climbing), luamethod(LuaLocalPlayer, swimming_vertical), luamethod(LuaLocalPlayer, get_physics_override), - luamethod(LuaLocalPlayer, get_override_pos), + // TODO: figure our if these are useful in any way luamethod(LuaLocalPlayer, get_last_pos), luamethod(LuaLocalPlayer, get_last_velocity), luamethod(LuaLocalPlayer, get_last_look_horizontal), luamethod(LuaLocalPlayer, get_last_look_vertical), - luamethod(LuaLocalPlayer, get_key_pressed), + // + luamethod(LuaLocalPlayer, get_control), luamethod(LuaLocalPlayer, get_breath), luamethod(LuaLocalPlayer, get_pos), luamethod(LuaLocalPlayer, get_movement_acceleration), luamethod(LuaLocalPlayer, get_movement_speed), luamethod(LuaLocalPlayer, get_movement), + luamethod(LuaLocalPlayer, get_armor_groups), luamethod(LuaLocalPlayer, hud_add), luamethod(LuaLocalPlayer, hud_remove), luamethod(LuaLocalPlayer, hud_change), diff --git a/src/script/lua_api/l_localplayer.h b/src/script/lua_api/l_localplayer.h index 01de2ed4e..4413f2bdb 100644 --- a/src/script/lua_api/l_localplayer.h +++ b/src/script/lua_api/l_localplayer.h @@ -32,12 +32,21 @@ private: // garbage collector static int gc_object(lua_State *L); + // get_velocity(self) static int l_get_velocity(lua_State *L); + // get_hp(self) static int l_get_hp(lua_State *L); + // get_name(self) static int l_get_name(lua_State *L); + // get_wield_index(self) + static int l_get_wield_index(lua_State *L); + + // get_wielded_item(self) + static int l_get_wielded_item(lua_State *L); + static int l_is_attached(lua_State *L); static int l_is_touching_ground(lua_State *L); static int l_is_in_liquid(lua_State *L); @@ -54,18 +63,28 @@ private: static int l_get_last_velocity(lua_State *L); static int l_get_last_look_vertical(lua_State *L); static int l_get_last_look_horizontal(lua_State *L); - static int l_get_key_pressed(lua_State *L); + // get_control(self) + static int l_get_control(lua_State *L); + + // get_breath(self) static int l_get_breath(lua_State *L); + // get_pos(self) static int l_get_pos(lua_State *L); + // get_movement_acceleration(self) static int l_get_movement_acceleration(lua_State *L); + // get_movement_speed(self) static int l_get_movement_speed(lua_State *L); + // get_movement(self) static int l_get_movement(lua_State *L); + // get_armor_groups(self) + static int l_get_armor_groups(lua_State *L); + // hud_add(self, id, form) static int l_hud_add(lua_State *L); -- cgit v1.2.3