Add EnvRef:set_timeofday(0...1) and EnvRef:get_timeofday()
authorPerttu Ahola <celeron55@gmail.com>
Tue, 24 Jan 2012 10:01:59 +0000 (12:01 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Tue, 24 Jan 2012 10:01:59 +0000 (12:01 +0200)
data/mods/default/init.lua
data/mods/experimental/init.lua
src/scriptapi.cpp

index 7c6cccd44761d6efb45aa633dd6912687483a4ff..f0e6b6dc230117c7a3f95f0afd4c2057fe4240f0 100644 (file)
 -- minetest.chat_send_player(name, text)
 -- minetest.get_player_privs(name) -> set of privs
 -- minetest.get_inventory(location) -> InvRef
--- minetest.get_current_modname() -> string
--- minetest.get_modpath(modname) -> eg. "/home/user/.minetest/usermods/modname"
 -- ^ location = eg. {type="player", name="celeron55"}
 --                  {type="node", pos={x=, y=, z=}}
+-- minetest.get_current_modname() -> string
+-- minetest.get_modpath(modname) -> eg. "/home/user/.minetest/usermods/modname"
 --
 -- minetest.debug(line)
 -- ^ Goes to dstream
 -- - get_meta(pos) -- Get a NodeMetaRef at that position
 -- - get_player_by_name(name) -- Get an ObjectRef to a player
 -- - get_objects_inside_radius(pos, radius)
+-- - set_timeofday(val): val: 0...1; 0 = midnight, 0.5 = midday
+-- - get_timeofday()
 --
 -- NodeMetaRef (this stuff is subject to change in a future version)
 -- - get_type()
index 368295ea7fb35b556974becaa4dff667c14e3966..9a8f8868d5ff31c049126d16ebd0346a56bd47ff 100644 (file)
@@ -4,6 +4,8 @@
 
 -- For testing random stuff
 
+experimental = {}
+
 function on_step(dtime)
        -- print("experimental on_step")
        --[[
@@ -20,6 +22,26 @@ function on_step(dtime)
                end
        end
        --]]
+       --[[
+       if experimental.t1 == nil then
+               experimental.t1 = 0
+       end
+       experimental.t1 = experimental.t1 + dtime
+       if experimental.t1 >= 2 then
+               experimental.t1 = experimental.t1 - 2
+               minetest.log("time of day is "..minetest.env:get_timeofday())
+               if experimental.day then
+                       minetest.log("forcing day->night")
+                       experimental.day = false
+                       minetest.env:set_timeofday(0.0)
+               else
+                       minetest.log("forcing night->day")
+                       experimental.day = true
+                       minetest.env:set_timeofday(0.5)
+               end
+               minetest.log("time of day is "..minetest.env:get_timeofday())
+       end
+       --]]
 end
 minetest.register_globalstep(on_step)
 
index 5d68793e248a619ab8802cc024860f19b0161c54..a064cd688a0b318cdac5c92fb5263c3389a333d6 100644 (file)
@@ -2913,6 +2913,38 @@ private:
                return 1;
        }
 
+       // EnvRef:set_timeofday(val)
+       // val = 0...1
+       static int l_set_timeofday(lua_State *L)
+       {
+               EnvRef *o = checkobject(L, 1);
+               ServerEnvironment *env = o->m_env;
+               if(env == NULL) return 0;
+               // Do it
+               float timeofday_f = luaL_checknumber(L, 2);
+               assert(timeofday_f >= 0.0 && timeofday_f <= 1.0);
+               int timeofday_mh = (int)(timeofday_f * 24000.0);
+               // This should be set directly in the environment but currently
+               // such changes aren't immediately sent to the clients, so call
+               // the server instead.
+               //env->setTimeOfDay(timeofday_mh);
+               get_server(L)->setTimeOfDay(timeofday_mh);
+               return 0;
+       }
+
+       // EnvRef:get_timeofday() -> 0...1
+       static int l_get_timeofday(lua_State *L)
+       {
+               EnvRef *o = checkobject(L, 1);
+               ServerEnvironment *env = o->m_env;
+               if(env == NULL) return 0;
+               // Do it
+               int timeofday_mh = env->getTimeOfDay();
+               float timeofday_f = (float)timeofday_mh / 24000.0;
+               lua_pushnumber(L, timeofday_f);
+               return 1;
+       }
+
        static int gc_object(lua_State *L) {
                EnvRef *o = *(EnvRef **)(lua_touserdata(L, 1));
                delete o;
@@ -2990,6 +3022,8 @@ const luaL_reg EnvRef::methods[] = {
        method(EnvRef, get_meta),
        method(EnvRef, get_player_by_name),
        method(EnvRef, get_objects_inside_radius),
+       method(EnvRef, set_timeofday),
+       method(EnvRef, get_timeofday),
        {0,0}
 };