]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
[CSM] Add `get_node` and `get_node_or_nil`
authorred-001 <red-001@outlook.ie>
Mon, 30 Jan 2017 19:10:37 +0000 (19:10 +0000)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Mon, 13 Mar 2017 22:56:05 +0000 (23:56 +0100)
clientmods/preview/init.lua
doc/client_lua_api.txt
src/client.cpp
src/client.h
src/script/lua_api/l_client.cpp
src/script/lua_api/l_client.h

index 3085eda247d231d28f619f8545a877bfd26a7c17..91b12d81047c9dd8bdad958a8d878765426adcd4 100644 (file)
@@ -39,6 +39,14 @@ core.register_chatcommand("dump", {
        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,
+})
+
+
 core.after(2, function()
        print("[PREVIEW] loaded " .. modname .. " mod")
 end)
@@ -47,4 +55,4 @@ core.register_on_dignode(function(pos, node)
        print("pos:" .. dump(pos))
        print("node:" .. dump(node))
        return false
-end)
\ No newline at end of file
+end)
index 886dbc6e1ce4a12423acc4509b15c4db50c7d519..7b24452e83ebf4aed8d5775db9ccbf6244f3e37b 100644 (file)
@@ -717,7 +717,13 @@ Call these functions only at load time!
 * `minetest.after(time, func, ...)`
     * Call the function `func` after `time` seconds, may be fractional
     * Optional: Variable number of arguments that are passed to `func`
-
+### Map
+* `minetest.get_node(pos)`
+    * Returns the node at the given position as table in the format
+      `{name="node_name", param1=0, param2=0}`, returns `{name="ignore", param1=0, param2=0}`
+      for unloaded areas.
+* `minetest.get_node_or_nil(pos)`
+    * Same as `get_node` but returns `nil` for unloaded areas.
 ### Misc.
 * `minetest.parse_json(string[, nullvalue])`: returns something
     * Convert a string containing JSON data into the Lua equivalent
index 4bb63fef1302be68319cb4141485af478a0638e2..049616c63c9dbb49f4b086a1bf06efdb21c1f5b6 100644 (file)
@@ -1447,6 +1447,11 @@ void Client::removeNode(v3s16 p)
        }
 }
 
+MapNode Client::getNode(v3s16 p, bool *is_valid_position)
+{
+       return m_env.getMap().getNodeNoEx(p, is_valid_position);
+}
+
 void Client::addNode(v3s16 p, MapNode n, bool remove_metadata)
 {
        //TimeTaker timer1("Client::addNode()");
index 9b7130268be5c78102d682d118a4139b6fa95c1d..dc4469350283b0f48aba91d01ea1fd7d11a97330 100644 (file)
@@ -441,6 +441,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
 
        // Causes urgent mesh updates (unlike Map::add/removeNodeWithEvent)
        void removeNode(v3s16 p);
+       MapNode getNode(v3s16 p, bool *is_valid_position);
        void addNode(v3s16 p, MapNode n, bool remove_metadata = true);
 
        void setPlayerControl(PlayerControl &control);
index 9a04bd02ff9a8350e4f0ce848fa85bd760b7b24b..41e33889cc45cc370547ddca1eb4add0a1486bd4 100644 (file)
@@ -23,6 +23,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "util/string.h"
 #include "cpp_api/s_base.h"
 #include "gettext.h"
+#include "common/c_converter.h"
+#include "common/c_content.h"
 
 int ModApiClient::l_get_current_modname(lua_State *L)
 {
@@ -97,6 +99,39 @@ int ModApiClient::l_gettext(lua_State *L)
        return 1;
 }
 
+// get_node(pos)
+// pos = {x=num, y=num, z=num}
+int ModApiClient::l_get_node(lua_State *L)
+{
+       // pos
+       v3s16 pos = read_v3s16(L, 1);
+       // Do it
+       bool pos_ok;
+       MapNode n = getClient(L)->getNode(pos, &pos_ok);
+       // Return node
+       pushnode(L, n, getClient(L)->ndef());
+       return 1;
+}
+
+// get_node_or_nil(pos)
+// pos = {x=num, y=num, z=num}
+int ModApiClient::l_get_node_or_nil(lua_State *L)
+{
+       // pos
+       v3s16 pos = read_v3s16(L, 1);
+       // Do it
+       bool pos_ok;
+       MapNode n = getClient(L)->getNode(pos, &pos_ok);
+       if (pos_ok) {
+               // Return node
+               pushnode(L, n, getClient(L)->ndef());
+       }
+       else {
+               lua_pushnil(L);
+       }
+       return 1;
+}
+
 void ModApiClient::Initialize(lua_State *L, int top)
 {
        API_FCT(get_current_modname);
@@ -106,4 +141,6 @@ void ModApiClient::Initialize(lua_State *L, int top)
        API_FCT(show_formspec);
        API_FCT(send_respawn);
        API_FCT(gettext);
+       API_FCT(get_node);
+       API_FCT(get_node_or_nil);
 }
index d0e230630c1cc54b9e3cd86f351296b16ef0d04a..207a5bca08701fb7975ecf46ef1bc16ccd05a3af 100644 (file)
@@ -47,6 +47,14 @@ class ModApiClient: public ModApiBase
        // set_last_run_mod(modname)
        static int l_set_last_run_mod(lua_State *L);
 
+       // get_node(pos)
+       static int l_get_node(lua_State *L);
+
+       // get_node_or_nil(pos)
+       static int l_get_node_or_nil(lua_State *L);
+
+
+
 public:
        static void Initialize(lua_State *L, int top);
 };