naev 0.12.5
ntime.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
32
34#include <stdio.h>
35#include <stdlib.h>
37
38#include "ntime.h"
39
40#include "economy.h"
41#include "hook.h"
42
43#define NT_SECONDS_DIV ( 1000 ) /* Divider for extracting seconds. */
44#define NT_SECONDS_DT \
45 ( 30 ) /* Update rate, how many seconds are in a real second. */
46#define NT_CYCLE_SECONDS \
47 ( (ntime_t)NT_CYCLE_PERIODS * \
48 (ntime_t)NT_PERIOD_SECONDS ) /* Seconds in a cycle */
49#define NT_PERIODS_DIV \
50 ( (ntime_t)NT_PERIOD_SECONDS * \
51 (ntime_t)NT_SECONDS_DIV ) /* Divider for extracting periods. */
52#define NT_CYCLES_DIV \
53 ( (ntime_t)NT_CYCLE_SECONDS * \
54 (ntime_t)NT_SECONDS_DIV ) /* Divider for extracting cycles. */
55
60typedef struct NTimeUpdate_s {
61 struct NTimeUpdate_s *next;
62 ntime_t inc;
65
66static ntime_t naev_time = 0;
67static double naev_remainder =
68 0.;
69static int ntime_enable = 1;
70
74void ntime_update( double dt )
75{
76 double dtt, tu;
77 ntime_t inc;
78
79 /* Only if we need to update. */
80 if ( !ntime_enable )
81 return;
82
83 /* Calculate the effective time. */
84 dtt = naev_remainder + dt * NT_SECONDS_DT * NT_SECONDS_DIV;
85
86 /* Time to update. */
87 tu = floor( dtt );
88 inc = (ntime_t)tu;
89 naev_remainder = dtt - tu; /* Leave remainder. */
90
91 /* Increment. */
92 naev_time += inc;
93 hooks_updateDate( inc );
94}
95
99ntime_t ntime_create( int scu, int stp, int stu )
100{
101 ntime_t tscu, tstp, tstu;
102 tscu = scu;
103 tstp = stp;
104 tstu = stu;
105 return tscu * NT_CYCLES_DIV + tstp * NT_PERIODS_DIV + tstu * NT_SECONDS_DIV;
106}
107
113ntime_t ntime_get( void )
114{
115 return naev_time;
116}
117
121void ntime_getR( int *cycles, int *periods, int *seconds, double *rem )
122{
123 *cycles = ntime_getCycles( naev_time );
124 *periods = ntime_getPeriods( naev_time );
125 *seconds = ntime_getSeconds( naev_time );
127}
128
132int ntime_getCycles( ntime_t t )
133{
134 return ( t / NT_CYCLES_DIV );
135}
136
140int ntime_getPeriods( ntime_t t )
141{
142 return ( t / NT_PERIODS_DIV ) % NT_CYCLE_PERIODS;
143}
144
148int ntime_getSeconds( ntime_t t )
149{
150 return ( t / NT_SECONDS_DIV ) % NT_PERIOD_SECONDS;
151}
152
158double ntime_convertSeconds( ntime_t t )
159{
160 return ( (double)t / (double)NT_SECONDS_DIV );
161}
162
166double ntime_getRemainder( ntime_t t )
167{
168 return (double)( t % NT_SECONDS_DIV );
169}
170
178char *ntime_pretty( ntime_t t, int d )
179{
180 char str[64];
181 ntime_prettyBuf( str, sizeof( str ), t, d );
182 return strdup( str );
183}
184
194void ntime_prettyBuf( char *str, int max, ntime_t t, int d )
195{
196 ntime_t nt;
197 int cycles, periods, seconds;
198
199 if ( t == 0 )
200 nt = naev_time;
201 else
202 nt = t;
203
204 /* UST (Universal Synchronized Time) - unit is seconds */
205 cycles = ntime_getCycles( nt );
206 periods = ntime_getPeriods( nt );
207 seconds = ntime_getSeconds( nt );
208 if ( ( cycles == 0 ) && ( periods == 0 ) ) /* only seconds */
209 snprintf( str, max, _( "%04d s" ), seconds );
210 else if ( ( cycles == 0 ) || ( d == 0 ) )
211 snprintf( str, max, _( "%.*f p" ), d, periods + 0.0001 * seconds );
212 else /* UST format */
213 snprintf( str, max, _( "UST %d:%.*f" ), cycles, d,
214 periods + 0.0001 * seconds );
215}
216
222void ntime_set( ntime_t t )
223{
224 naev_time = t;
225 naev_remainder = 0.;
226}
227
231void ntime_setR( int cycles, int periods, int seconds, double rem )
232{
233 naev_time = ntime_create( cycles, periods, seconds );
234 naev_time += floor( rem );
235 naev_remainder = fmod( rem, 1. );
236}
237
243void ntime_inc( ntime_t t )
244{
245 naev_time += t;
246 economy_update( t );
247
248 /* Run hooks. */
249 if ( t > 0 )
250 hooks_updateDate( t );
251}
252
258void ntime_allowUpdate( int enable )
259{
260 ntime_enable = enable;
261}
262
271void ntime_incLagged( ntime_t t )
272{
273 NTimeUpdate_t *ntu, *iter;
274
275 /* Create the time increment. */
276 ntu = malloc( sizeof( NTimeUpdate_t ) );
277 ntu->next = NULL;
278 ntu->inc = t;
279
280 /* Only member. */
281 if ( ntime_inclist == NULL )
282 ntime_inclist = ntu;
283
284 else {
285 /* Find end of list. */
286 for ( iter = ntime_inclist; iter->next != NULL; iter = iter->next )
287 ;
288 /* Append to end. */
289 iter->next = ntu;
290 }
291}
292
296void ntime_refresh( void )
297{
298 NTimeUpdate_t *ntu;
299
300 /* We have to run all the increments one by one to ensure all hooks get
301 * run and that no collisions occur. */
302 while ( ntime_inclist != NULL ) {
303 ntu = ntime_inclist;
304
305 /* Run hook stuff and actually update time. */
306 naev_time += ntu->inc;
307 economy_update( ntu->inc );
308
309 /* Remove the increment. */
310 ntime_inclist = ntu->next;
311
312 /* Free the increment. */
313 free( ntu );
314 }
315}
int economy_update(unsigned int dt)
Updates the economy.
Definition economy.c:506
void hooks_updateDate(ntime_t change)
Updates the time to see if it should be updated.
Definition hook.c:688
void ntime_set(ntime_t t)
Sets the time absolutely, does NOT generate an event, used at init.
Definition ntime.c:222
ntime_t ntime_create(int scu, int stp, int stu)
Creates a time structure.
Definition ntime.c:99
static NTimeUpdate_t * ntime_inclist
Definition ntime.c:64
ntime_t ntime_get(void)
Gets the current time.
Definition ntime.c:113
char * ntime_pretty(ntime_t t, int d)
Gets the time in a pretty human readable format.
Definition ntime.c:178
static double naev_remainder
Definition ntime.c:67
int ntime_getSeconds(ntime_t t)
Gets the seconds of a time.
Definition ntime.c:148
void ntime_refresh(void)
Checks to see if ntime has any hooks pending to run.
Definition ntime.c:296
void ntime_inc(ntime_t t)
Sets the time relatively.
Definition ntime.c:243
void ntime_getR(int *cycles, int *periods, int *seconds, double *rem)
Gets the current time broken into individual components.
Definition ntime.c:121
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
void ntime_update(double dt)
Updatse the time based on realtime.
Definition ntime.c:74
void ntime_allowUpdate(int enable)
Allows the time to update when the game is updating.
Definition ntime.c:258
int ntime_getCycles(ntime_t t)
Gets the cycles of a time.
Definition ntime.c:132
void ntime_setR(int cycles, int periods, int seconds, double rem)
Loads time including remainder.
Definition ntime.c:231
int ntime_getPeriods(ntime_t t)
Gets the periods of a time.
Definition ntime.c:140
double ntime_getRemainder(ntime_t t)
Gets the remainder.
Definition ntime.c:166
static ntime_t naev_time
Definition ntime.c:66
double ntime_convertSeconds(ntime_t t)
Converts the time to seconds.
Definition ntime.c:158
void ntime_incLagged(ntime_t t)
Sets the time relatively.
Definition ntime.c:271
static const double d[]
Definition rng.c:263
Used for storing time increments to not trigger hooks during Lua calls and such.
Definition ntime.c:60
ntime_t inc
Definition ntime.c:62
struct NTimeUpdate_s * next
Definition ntime.c:61