diff --git a/src/misn_lua.c b/src/misn_lua.c index bc0f089..20e33e7 100644 --- a/src/misn_lua.c +++ b/src/misn_lua.c @@ -11,6 +11,7 @@ #include "toolkit.h" #include "land.h" #include "player.h" +#include "ltime.h" #include "misn_lua.h" #define MISN_DEBUG(str, args...) (fprintf(stdout, "Mission '%s': "str"\n", \ @@ -96,6 +97,17 @@ static const luaL_Reg space_methods[] = { { 0, 0 } }; +// Time. +static int time_get(lua_State* L); +static int time_str(lua_State* L); +static int time_units(lua_State* L); +static const luaL_reg time_methods[] = { + { "get", time_get }, + { "str", time_str }, + { "units", time_units }, + {0, 0} +}; + // Player. static int player_freeSpace(lua_State* L); static int player_addCargo(lua_State* L); @@ -139,6 +151,7 @@ int misn_loadLibs(lua_State* L) { luaL_register(L, "misn", misn_methods); luaL_register(L, "var", var_methods); luaL_register(L, "space", space_methods); + luaL_register(L, "time", time_methods); luaL_register(L, "player", player_methods); luaL_register(L, "rnd", rnd_methods); luaL_register(L, "hook", hook_methods); @@ -502,6 +515,31 @@ static int space_landName(lua_State* L) { return 0; } +// -- Time. -- +static int time_get(lua_State* L) { + lua_pushnumber(L, ltime_get()); + return 1; +} + +static int time_str(lua_State* L) { + char* lt; + if((lua_gettop(L) > 0) && (lua_isnumber(L, -1))) + lt = ltime_pretty((unsigned int) lua_tonumber(L, -1)); + else + lt = ltime_pretty(ltime_get()); + lua_pushstring(L, lt); + free(lt); + return 1; +} + +static int time_units(lua_State* L) { + if((lua_gettop(L) > 0) && (lua_isnumber(L, -1))) + lua_pushnumber(L, (unsigned int) lua_tonumber(L, -1) * LTIME_UNIT_LENGTH); + else + lua_pushnumber(L, LTIME_UNIT_LENGTH); + return 1; +} + // -- Player. -- static int player_freeSpace(lua_State* L) { lua_pushnumber(L, pilot_freeCargo(player));