naev 0.11.5
nlua_time.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include <lauxlib.h>
11#include <stdlib.h>
12
13#include "naev.h"
16#include "nlua_time.h"
17
18#include "log.h"
19#include "nluadef.h"
20#include "ntime.h"
21
22/* Time methods. */
23static int timeL_new( lua_State *L );
24static int timeL_add( lua_State *L );
25static int timeL_add__( lua_State *L );
26static int timeL_sub( lua_State *L );
27static int timeL_sub__( lua_State *L );
28static int timeL_eq( lua_State *L );
29static int timeL_lt( lua_State *L );
30static int timeL_le( lua_State *L );
31static int timeL_get( lua_State *L );
32static int timeL_str( lua_State *L );
33static int timeL_inc( lua_State *L );
34static int timeL_tonumber( lua_State *L );
35static int timeL_fromnumber( lua_State *L );
36static const luaL_Reg time_methods[] = {
37 { "new", timeL_new },
38 { "add", timeL_add__ },
39 { "__add", timeL_add },
40 { "sub", timeL_sub__ },
41 { "__sub", timeL_sub },
42 { "__eq", timeL_eq },
43 { "__lt", timeL_lt },
44 { "__le", timeL_le },
45 { "get", timeL_get },
46 { "str", timeL_str },
47 { "__tostring", timeL_str },
48 { "inc", timeL_inc },
49 { "tonumber", timeL_tonumber },
50 { "fromnumber", timeL_fromnumber },
51 {0,0}
52};
60int nlua_loadTime( nlua_env env )
61{
62 nlua_register(env, TIME_METATABLE, time_methods, 1);
63 return 0; /* No error */
64}
65
90ntime_t* lua_totime( lua_State *L, int ind )
91{
92 return (ntime_t*) lua_touserdata(L,ind);
93}
101ntime_t* luaL_checktime( lua_State *L, int ind )
102{
103 if (lua_istime(L,ind))
104 return lua_totime(L,ind);
105 luaL_typerror(L, ind, TIME_METATABLE);
106 return NULL;
107}
115ntime_t luaL_validtime( lua_State *L, int ind )
116{
117 return *luaL_checktime( L, ind );
118}
126ntime_t* lua_pushtime( lua_State *L, ntime_t time )
127{
128 ntime_t *p = (ntime_t*) lua_newuserdata(L, sizeof(ntime_t));
129 *p = time;
130 luaL_getmetatable(L, TIME_METATABLE);
131 lua_setmetatable(L, -2);
132 return p;
133}
141int lua_istime( lua_State *L, int ind )
142{
143 int ret;
144
145 if (lua_getmetatable(L,ind)==0)
146 return 0;
147 lua_getfield(L, LUA_REGISTRYINDEX, TIME_METATABLE);
148
149 ret = 0;
150 if (lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
151 ret = 1;
152
153 lua_pop(L, 2); /* remove both metatables */
154 return ret;
155}
156
168static int timeL_new( lua_State *L )
169{
170 int cycles, periods, seconds;
171
172 /* Parameters. */
173 cycles = luaL_checkint(L,1);
174 periods = luaL_checkint(L,2);
175 seconds = luaL_checkint(L,3);
176
177 /* Create the time. */
178 lua_pushtime( L, ntime_create( cycles, periods, seconds ) );
179 return 1;
180}
192static int timeL_add( lua_State *L )
193{
194 ntime_t t1, t2;
195
196 /* Parameters. */
197 t1 = luaL_validtime( L, 1 );
198 t2 = luaL_validtime( L, 2 );
199
200 /* Add them. */
201 lua_pushtime( L, t1 + t2 );
202 return 1;
203}
204
205/*
206 * Method version of time_add that modifies the first time.
207 */
208static int timeL_add__( lua_State *L )
209{
210 ntime_t *t1, t2;
211
212 /* Parameters. */
213 t1 = luaL_checktime( L, 1 );
214 t2 = luaL_validtime( L, 2 );
215
216 /* Add them. */
217 *t1 += t2;
218 lua_pushtime( L, *t1 );
219 return 1;
220}
221
233static int timeL_sub( lua_State *L )
234{
235 ntime_t t1, t2;
236
237 /* Parameters. */
238 t1 = luaL_validtime( L, 1 );
239 t2 = luaL_validtime( L, 2 );
240
241 /* Sub them. */
242 lua_pushtime( L, t1 - t2 );
243 return 1;
244}
245
246/*
247 * Method version of time_sub that modifies the first time.
248 */
249static int timeL_sub__( lua_State *L )
250{
251 ntime_t *t1, t2;
252
253 /* Parameters. */
254 t1 = luaL_checktime( L, 1 );
255 t2 = luaL_validtime( L, 2 );
256
257 /* Sub them. */
258 *t1 -= t2;
259 lua_pushtime( L, *t1 );
260 return 1;
261}
262
275static int timeL_eq( lua_State *L )
276{
277 ntime_t t1, t2;
278 t1 = luaL_validtime( L, 1 );
279 t2 = luaL_validtime( L, 2 );
280 lua_pushboolean( L, t1==t2 );
281 return 1;
282}
293static int timeL_lt( lua_State *L )
294{
295 ntime_t t1, t2;
296 t1 = luaL_validtime( L, 1 );
297 t2 = luaL_validtime( L, 2 );
298 lua_pushboolean( L, t1<t2 );
299 return 1;
300}
311static int timeL_le( lua_State *L )
312{
313 ntime_t t1, t2;
314 t1 = luaL_validtime( L, 1 );
315 t2 = luaL_validtime( L, 2 );
316 lua_pushboolean( L, t1<=t2 );
317 return 1;
318}
327static int timeL_get( lua_State *L )
328{
329 lua_pushtime( L, ntime_get() );
330 return 1;
331}
345static int timeL_str( lua_State *L )
346{
347 ntime_t t;
348 char nt[64];
349 int d;
350
351 /* Parse parameters. */
352 if (!lua_isnoneornil(L,1))
353 t = luaL_validtime(L,1);
354 else
355 t = ntime_get();
356 d = luaL_optinteger(L,2,2); /* Defaults to 2 decimals. */
357
358 /* Push string. */
359 ntime_prettyBuf( nt, sizeof(nt), t, d );
360 lua_pushstring(L, nt);
361 return 1;
362}
363
374static int timeL_inc( lua_State *L )
375{
377 return 0;
378}
379
391static int timeL_tonumber( lua_State *L )
392{
393 ntime_t t = luaL_validtime(L,1);
394 lua_pushnumber( L, t );
395 return 1;
396}
397
409static int timeL_fromnumber( lua_State *L )
410{
411 ntime_t t = (ntime_t) luaL_checknumber(L,1);
412 lua_pushtime( L, t );
413 return 1;
414}
Header file with generic functions and naev-specifics.
static int timeL_sub(lua_State *L)
Subtracts two time metatables.
Definition nlua_time.c:233
static int timeL_lt(lua_State *L)
Checks to see if a time is strictly larger than another.
Definition nlua_time.c:293
ntime_t * luaL_checktime(lua_State *L, int ind)
Gets time at index raising an error if isn't a time.
Definition nlua_time.c:101
ntime_t * lua_pushtime(lua_State *L, ntime_t time)
Pushes a time on the stack.
Definition nlua_time.c:126
int nlua_loadTime(nlua_env env)
Loads the Time Lua library.
Definition nlua_time.c:60
int lua_istime(lua_State *L, int ind)
Checks to see if ind is a time.
Definition nlua_time.c:141
static int timeL_le(lua_State *L)
Checks to see if a time is larger or equal to another.
Definition nlua_time.c:311
static int timeL_tonumber(lua_State *L)
Gets a number representing this time.
Definition nlua_time.c:391
static int timeL_add(lua_State *L)
Adds two time metatables.
Definition nlua_time.c:192
static int timeL_fromnumber(lua_State *L)
Creates a time from a number representing it.
Definition nlua_time.c:409
static int timeL_new(lua_State *L)
Creates a time. This can be absolute or relative.
Definition nlua_time.c:168
static const luaL_Reg time_methods[]
Definition nlua_time.c:36
static int timeL_inc(lua_State *L)
Increases or decreases the in-game time.
Definition nlua_time.c:374
static int timeL_eq(lua_State *L)
Checks to see if two time are equal.
Definition nlua_time.c:275
static int timeL_str(lua_State *L)
Converts the time to a pretty human readable format.
Definition nlua_time.c:345
ntime_t luaL_validtime(lua_State *L, int ind)
Gets a time directly.
Definition nlua_time.c:115
static int timeL_get(lua_State *L)
Gets the current time in internal representation time.
Definition nlua_time.c:327
ntime_t * lua_totime(lua_State *L, int ind)
Bindings for interacting with the time.
Definition nlua_time.c:90
ntime_t ntime_create(int scu, int stp, int stu)
Creates a time structure.
Definition ntime.c:94
ntime_t ntime_get(void)
Gets the current time.
Definition ntime.c:108
void ntime_inc(ntime_t t)
Sets the time relatively.
Definition ntime.c:236
void ntime_prettyBuf(char *str, int max, ntime_t t, int d)
Gets the time in a pretty human readable format filling a preset buffer.
Definition ntime.c:188
static const double d[]
Definition rng.c:273