LUA function: get_time_of_day

For people working on the C++ code.
Post Reply
randomproof
Member
Posts: 214
Joined: Thu Nov 17, 2011 06:31
Location: California, USA

LUA function: get_time_of_day

by randomproof » Post

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.

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;
    }
Last edited by randomproof on Thu Jan 12, 2012 23:34, edited 1 time in total.

jn
Member
Posts: 106
Joined: Tue Jan 03, 2012 19:15

by jn » Post

lua_pushinteger can't throw an InvalidPositionException, AFAIK. (just nitpicking)

randomproof
Member
Posts: 214
Joined: Thu Nov 17, 2011 06:31
Location: California, USA

by randomproof » Post

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

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.
Last edited by randomproof on Thu Jan 12, 2012 23:40, edited 1 time in total.

sfan5
Moderator
Posts: 4095
Joined: Wed Aug 24, 2011 09:44
GitHub: sfan5
IRC: sfan5
Location: Germany

by sfan5 » Post

+2 for this Patch
Mods: Mesecons | WorldEdit | Nuke & Minetest builds for Windows (32-bit & 64-bit)

User avatar
Hackeridze
Member
Posts: 310
Joined: Thu Nov 03, 2011 13:35

by Hackeridze » Post

+1
My game: RTMG
GENTOO USER

sapier
Developer
Posts: 763
Joined: Tue Aug 16, 2011 18:17

by sapier » Post

+1
DON'T mention coding style!
(c) sapier all rights reserved

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests