Support static_spawnpoint setting
authorPerttu Ahola <celeron55@gmail.com>
Mon, 9 Apr 2012 20:29:55 +0000 (23:29 +0300)
committerPerttu Ahola <celeron55@gmail.com>
Mon, 9 Apr 2012 20:32:13 +0000 (23:32 +0300)
builtin/builtin.lua
builtin/chatcommands.lua
builtin/misc.lua
builtin/static_spawn.lua [new file with mode: 0644]
doc/lua_api.txt

index 10acd0f521ef3c5762a3ea028eca774386d5f788..13c1c09d4dca41ce1d27d9fd67a84d0898adad94 100644 (file)
@@ -19,4 +19,5 @@ dofile(minetest.get_modpath("__builtin").."/misc.lua")
 dofile(minetest.get_modpath("__builtin").."/privileges.lua")
 dofile(minetest.get_modpath("__builtin").."/auth.lua")
 dofile(minetest.get_modpath("__builtin").."/chatcommands.lua")
+dofile(minetest.get_modpath("__builtin").."/static_spawn.lua")
 
index 047cfc69218a0acd2010749e3cf95d1810427040..a7061e3a3f0d47dc8222115206d68cc0b54887b2 100644 (file)
@@ -322,7 +322,7 @@ minetest.register_chatcommand("set", {
        privs = {server=true},
        func = function(name, param)
                local arg, setname, setvalue = string.match(param, "(-[n]) ([^ ]+) (.+)")
-               if arg and arg == "n" and setname and setvalue then
+               if arg and arg == "-n" and setname and setvalue then
                        minetest.setting_set(setname, setvalue)
                        minetest.chat_send_player(name, setname.." = "..setvalue)
                        return
index 89f90dfbda4aa7a4776bdac2539bfb9e8d9fbeba..b3b1ec6e835150d553668421f4eb37d86fb233f4 100644 (file)
@@ -67,3 +67,35 @@ function minetest.get_node_group(name, group)
        return minetest.get_item_group(name, group)
 end
 
+function minetest.string_to_pos(value)
+       local p = {}
+       p.x, p.y, p.z = string.match(value, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
+       if p.x and p.y and p.z then
+               p.x = tonumber(p.x)
+               p.y = tonumber(p.y)
+               p.z = tonumber(p.z)
+               return p
+       end
+       local p = {}
+       p.x, p.y, p.z = string.match(value, "^%( *([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+) *%)$")
+       if p.x and p.y and p.z then
+               p.x = tonumber(p.x)
+               p.y = tonumber(p.y)
+               p.z = tonumber(p.z)
+               return p
+       end
+       return nil
+end
+
+assert(minetest.string_to_pos("10.0, 5, -2").x == 10)
+assert(minetest.string_to_pos("( 10.0, 5, -2)").z == -2)
+assert(minetest.string_to_pos("asd, 5, -2)") == nil)
+
+function minetest.setting_get_pos(name)
+       local value = minetest.setting_get(name)
+       if not value then
+               return nil
+       end
+       return minetest.string_to_pos(value)
+end
+
diff --git a/builtin/static_spawn.lua b/builtin/static_spawn.lua
new file mode 100644 (file)
index 0000000..e8c107d
--- /dev/null
@@ -0,0 +1,33 @@
+-- Minetest: builtin/static_spawn.lua
+
+local function warn_invalid_static_spawnpoint()
+       if minetest.setting_get("static_spawnpoint") and
+                       not minetest.setting_get_pos("static_spawnpoint") then
+               minetest.log('error', "The static_spawnpoint setting is invalid: \""..
+                               minetest.setting_get("static_spawnpoint").."\"")
+       end
+end
+
+warn_invalid_static_spawnpoint()
+
+local function put_player_in_spawn(obj)
+       warn_invalid_static_spawnpoint()
+       local static_spawnpoint = minetest.setting_get_pos("static_spawnpoint")
+       if not static_spawnpoint then
+               return false
+       end
+       minetest.log('action', "Moving "..obj:get_player_name()..
+                       " to static spawnpoint at "..
+                       minetest.pos_to_string(static_spawnpoint))
+       obj:setpos(static_spawnpoint)
+       return true
+end
+
+minetest.register_on_newplayer(function(obj)
+       put_player_in_spawn(obj)
+end)
+
+minetest.register_on_respawnplayer(function(obj)
+       return put_player_in_spawn(obj)
+end)
+
index aabc673f9357541827a70634375d49b3310b4a68..9bb0dfe98863e7d83033a151434f9ea6bb9f2a2d 100644 (file)
@@ -514,6 +514,7 @@ string:trim()
 ^ eg. string.trim("\n \t\tfoo bar\t ") == "foo bar"
 minetest.pos_to_string({x=X,y=Y,z=Z}) -> "(X,Y,Z)"
 ^ Convert position to a printable string
+minetest.string_to_pos(string) -> position
 
 minetest namespace reference
 -----------------------------
@@ -576,6 +577,7 @@ Setting-related:
 minetest.setting_set(name, value)
 minetest.setting_get(name) -> string or nil
 minetest.setting_getbool(name) -> boolean value or nil
+minetest.setting_get_pos(name) -> position or nil
 minetest.add_to_creative_inventory(itemstring)
 
 Authentication: