LUA function: get_time_of_day

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

LUA function: get_time_of_day

by randomproof » Thu Jan 12, 2012 19:39

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;
    }
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 » Thu Jan 12, 2012 20:58

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

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

by randomproof » Thu Jan 12, 2012 23:31

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

User avatar
sfan5
Member
 
Posts: 3636
Joined: Wed Aug 24, 2011 09:44
GitHub: sfan5
IRC: sfan5

by sfan5 » Fri Jan 13, 2012 06:30

+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 » Fri Jan 13, 2012 08:51

+1
My game: RTMG
GENTOO USER
 

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

by sapier » Fri Jan 13, 2012 10:56

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


Return to Minetest Engine

Who is online

Users browsing this forum: No registered users and 3 guests

cron