naev 0.12.6
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>
13
14#include "nlua_time.h"
15
16#include "nluadef.h"
17#include "ntime.h"
18
19/* Time methods. */
20static int timeL_new( lua_State *L );
21static int timeL_add( lua_State *L );
22static int timeL_add__( lua_State *L );
23static int timeL_sub( lua_State *L );
24static int timeL_sub__( lua_State *L );
25static int timeL_eq( lua_State *L );
26static int timeL_lt( lua_State *L );
27static int timeL_le( lua_State *L );
28static int timeL_get( lua_State *L );
29static int timeL_str( lua_State *L );
30static int timeL_inc( lua_State *L );
31static int timeL_tonumber( lua_State *L );
32static int timeL_fromnumber( lua_State *L );
33
34static const luaL_Reg time_methods[] = { { "new", timeL_new },
35 { "add", timeL_add__ },
36 { "__add", timeL_add },
37 { "sub", timeL_sub__ },
38 { "__sub", timeL_sub },
39 { "__eq", timeL_eq },
40 { "__lt", timeL_lt },
41 { "__le", timeL_le },
42 { "get", timeL_get },
43 { "str", timeL_str },
44 { "__tostring", timeL_str },
45 { "inc", timeL_inc },
46 { "tonumber", timeL_tonumber },
47 { "fromnumber", timeL_fromnumber },
48 { 0, 0 } };
49
56int nlua_loadTime( nlua_env env )
57{
58 nlua_register( env, TIME_METATABLE, time_methods, 1 );
59 return 0; /* No error */
60}
61
87ntime_t *lua_totime( lua_State *L, int ind )
88{
89 return (ntime_t *)lua_touserdata( L, ind );
90}
91
98ntime_t *luaL_checktime( lua_State *L, int ind )
99{
100 if ( lua_istime( L, ind ) )
101 return lua_totime( L, ind );
102 luaL_typerror( L, ind, TIME_METATABLE );
103 return NULL;
104}
105
112ntime_t luaL_validtime( lua_State *L, int ind )
113{
114 return *luaL_checktime( L, ind );
115}
116
123ntime_t *lua_pushtime( lua_State *L, ntime_t time )
124{
125 ntime_t *p = (ntime_t *)lua_newuserdata( L, sizeof( ntime_t ) );
126 *p = time;
127 luaL_getmetatable( L, TIME_METATABLE );
128 lua_setmetatable( L, -2 );
129 return p;
130}
131
138int lua_istime( lua_State *L, int ind )
139{
140 int ret;
141
142 if ( lua_getmetatable( L, ind ) == 0 )
143 return 0;
144 lua_getfield( L, LUA_REGISTRYINDEX, TIME_METATABLE );
145
146 ret = 0;
147 if ( lua_rawequal( L, -1, -2 ) ) /* does it have the correct mt? */
148 ret = 1;
149
150 lua_pop( L, 2 ); /* remove both metatables */
151 return ret;
152}
153
166static int timeL_new( lua_State *L )
167{
168 int cycles, periods, seconds;
169
170 /* Parameters. */
171 cycles = luaL_checkint( L, 1 );
172 periods = luaL_checkint( L, 2 );
173 seconds = luaL_checkint( L, 3 );
174
175 /* Create the time. */
176 lua_pushtime( L, ntime_create( cycles, periods, seconds ) );
177 return 1;
178}
179
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
235static int timeL_sub( lua_State *L )
236{
237 ntime_t t1, t2;
238
239 /* Parameters. */
240 t1 = luaL_validtime( L, 1 );
241 t2 = luaL_validtime( L, 2 );
242
243 /* Sub them. */
244 lua_pushtime( L, t1 - t2 );
245 return 1;
246}
247
248/*
249 * Method version of time_sub that modifies the first time.
250 */
251static int timeL_sub__( lua_State *L )
252{
253 ntime_t *t1, t2;
254
255 /* Parameters. */
256 t1 = luaL_checktime( L, 1 );
257 t2 = luaL_validtime( L, 2 );
258
259 /* Sub them. */
260 *t1 -= t2;
261 lua_pushtime( L, *t1 );
262 return 1;
263}
264
278static int timeL_eq( lua_State *L )
279{
280 ntime_t t1, t2;
281 t1 = luaL_validtime( L, 1 );
282 t2 = luaL_validtime( L, 2 );
283 lua_pushboolean( L, t1 == t2 );
284 return 1;
285}
286
297static int timeL_lt( lua_State *L )
298{
299 ntime_t t1, t2;
300 t1 = luaL_validtime( L, 1 );
301 t2 = luaL_validtime( L, 2 );
302 lua_pushboolean( L, t1 < t2 );
303 return 1;
304}
305
316static int timeL_le( lua_State *L )
317{
318 ntime_t t1, t2;
319 t1 = luaL_validtime( L, 1 );
320 t2 = luaL_validtime( L, 2 );
321 lua_pushboolean( L, t1 <= t2 );
322 return 1;
323}
324
332static int timeL_get( lua_State *L )
333{
334 lua_pushtime( L, ntime_get() );
335 return 1;
336}
337
353static int timeL_str( lua_State *L )
354{
355 ntime_t t;
356 char nt[64];
357 int d;
358
359 /* Parse parameters. */
360 if ( !lua_isnoneornil( L, 1 ) )
361 t = luaL_validtime( L, 1 );
362 else
363 t = ntime_get();
364 d = luaL_optinteger( L, 2, 2 ); /* Defaults to 2 decimals. */
365
366 /* Push string. */
367 ntime_prettyBuf( nt, sizeof( nt ), t, d );
368 lua_pushstring( L, nt );
369 return 1;
370}
371
382static int timeL_inc( lua_State *L )
383{
384 ntime_inc( luaL_validtime( L, 1 ) );
385 return 0;
386}
387
399static int timeL_tonumber( lua_State *L )
400{
401 ntime_t t = luaL_validtime( L, 1 );
402 lua_pushnumber( L, t );
403 return 1;
404}
405
417static int timeL_fromnumber( lua_State *L )
418{
419 ntime_t t = (ntime_t)luaL_checknumber( L, 1 );
420 lua_pushtime( L, t );
421 return 1;
422}
static int timeL_sub(lua_State *L)
Subtracts two time metatables.
Definition nlua_time.c:235
static int timeL_lt(lua_State *L)
Checks to see if a time is strictly larger than another.
Definition nlua_time.c:297
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:98
ntime_t * lua_pushtime(lua_State *L, ntime_t time)
Pushes a time on the stack.
Definition nlua_time.c:123
int nlua_loadTime(nlua_env env)
Loads the Time Lua library.
Definition nlua_time.c:56
int lua_istime(lua_State *L, int ind)
Checks to see if ind is a time.
Definition nlua_time.c:138
static int timeL_le(lua_State *L)
Checks to see if a time is larger or equal to another.
Definition nlua_time.c:316
static int timeL_tonumber(lua_State *L)
Gets a number representing this time.
Definition nlua_time.c:399
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:417
static int timeL_new(lua_State *L)
Creates a time. This can be absolute or relative.
Definition nlua_time.c:166
static const luaL_Reg time_methods[]
Definition nlua_time.c:34
static int timeL_inc(lua_State *L)
Increases or decreases the in-game time.
Definition nlua_time.c:382
static int timeL_eq(lua_State *L)
Checks to see if two time are equal.
Definition nlua_time.c:278
static int timeL_str(lua_State *L)
Converts the time to a pretty human readable format.
Definition nlua_time.c:353
ntime_t luaL_validtime(lua_State *L, int ind)
Gets a time directly.
Definition nlua_time.c:112
static int timeL_get(lua_State *L)
Gets the current time in internal representation time.
Definition nlua_time.c:332
ntime_t * lua_totime(lua_State *L, int ind)
Bindings for interacting with the time.
Definition nlua_time.c:87
ntime_t ntime_create(int scu, int stp, int stu)
Creates a time structure.
Definition ntime.c:99
ntime_t ntime_get(void)
Gets the current time.
Definition ntime.c:113
void ntime_inc(ntime_t t)
Sets the time relatively.
Definition ntime.c:243
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:194
static const double d[]
Definition rng.c:263