Page 1 of 1

LUA function: get_time_of_day

PostPosted: Thu Jan 12, 2012 19:39
by randomproof
Here is function I made so I could add a clock mod. It would be nice to have this added upstream.

clock mod link in case anyone wants to check it out.

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
const char EnvRef::className[] = "EnvRef";
const luaL_reg EnvRef::methods[] = {
    method(EnvRef, add_node),
    method(EnvRef, remove_node),
    method(EnvRef, get_node),
    method(EnvRef, get_node_or_nil),
    method(EnvRef, get_node_light),
    method(EnvRef, get_time_of_day),      <---  Add this
    method(EnvRef, add_entity),
    method(EnvRef, add_item),
    method(EnvRef, add_rat),
    method(EnvRef, add_firefly),
    method(EnvRef, get_meta),
    method(EnvRef, get_player_by_name),
    method(EnvRef, get_objects_inside_radius),
    {0,0}
};

    // EnvRef:get_time_of_day()
    static int l_get_time_of_day(lua_State *L)
    {
        EnvRef *o = checkobject(L, 1);
        ServerEnvironment *env = o->m_env;
        if(env == NULL) return 0;
        // Do it
        u32 time_of_day = env->getTimeOfDay();
        time_of_day %= 24000;
        lua_pushinteger(L, time_of_day);
        return 1;
    }

PostPosted: Thu Jan 12, 2012 20:58
by jn
lua_pushinteger can't throw an InvalidPositionException, AFAIK. (just nitpicking)

PostPosted: Thu Jan 12, 2012 23:31
by randomproof
jn wrote:lua_pushinteger can't throw an InvalidPositionException, AFAIK. (just nitpicking)

Fixed. Thanks. I just copied another function to make this one, so I didn't mess with the error checking already there.

Also, updated clock mod to add
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local clock_interval = math.min(60, (3600 / (tonumber(minetest.setting_get("time_speed")))) / 3)

so that the abm runs 3 times in a game hour or at least every 60 seconds. You can tweak with that if you want, but for the default time_speed it should be good.

PostPosted: Fri Jan 13, 2012 06:30
by sfan5
+2 for this Patch

PostPosted: Fri Jan 13, 2012 08:51
by Hackeridze
+1

PostPosted: Fri Jan 13, 2012 10:56
by sapier
+1