naev 0.11.5
ntime.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
34#include <stdio.h>
35#include <stdlib.h>
36
37#include "naev.h"
40#include "ntime.h"
41
42#include "economy.h"
43#include "hook.h"
44#include "nstring.h"
45
46#define NT_SECONDS_DIV (1000) /* Divider for extracting seconds. */
47#define NT_SECONDS_DT (30) /* Update rate, how many seconds are in a real second. */
48#define NT_CYCLE_SECONDS ((ntime_t)NT_CYCLE_PERIODS*(ntime_t)NT_PERIOD_SECONDS) /* Seconds in a cycle */
49#define NT_PERIODS_DIV ((ntime_t)NT_PERIOD_SECONDS*(ntime_t)NT_SECONDS_DIV) /* Divider for extracting periods. */
50#define NT_CYCLES_DIV ((ntime_t)NT_CYCLE_SECONDS*(ntime_t)NT_SECONDS_DIV) /* Divider for extracting cycles. */
51
56typedef struct NTimeUpdate_s {
57 struct NTimeUpdate_s *next;
58 ntime_t inc;
62static ntime_t naev_time = 0;
63static double naev_remainder = 0.;
64static int ntime_enable = 1;
69void ntime_update( double dt )
70{
71 double dtt, tu;
72 ntime_t inc;
73
74 /* Only if we need to update. */
75 if (!ntime_enable)
76 return;
77
78 /* Calculate the effective time. */
79 dtt = naev_remainder + dt*NT_SECONDS_DT*NT_SECONDS_DIV;
80
81 /* Time to update. */
82 tu = floor( dtt );
83 inc = (ntime_t) tu;
84 naev_remainder = dtt - tu; /* Leave remainder. */
85
86 /* Increment. */
87 naev_time += inc;
88 hooks_updateDate( inc );
89}
90
94ntime_t ntime_create( int scu, int stp, int stu )
95{
96 ntime_t tscu, tstp, tstu;
97 tscu = scu;
98 tstp = stp;
99 tstu = stu;
100 return tscu*NT_CYCLES_DIV + tstp*NT_PERIODS_DIV + tstu*NT_SECONDS_DIV;
101}
102
108ntime_t ntime_get (void)
109{
110 return naev_time;
111}
112
116void ntime_getR( int *cycles, int *periods, int *seconds, double *rem )
117{
118 *cycles = ntime_getCycles( naev_time );
119 *periods = ntime_getPeriods( naev_time );
120 *seconds = ntime_getSeconds( naev_time );
122}
123
127int ntime_getCycles( ntime_t t )
128{
129 return (t / NT_CYCLES_DIV);
130}
131
135int ntime_getPeriods( ntime_t t )
136{
137 return (t / NT_PERIODS_DIV) % NT_CYCLE_PERIODS;
138}
139
143int ntime_getSeconds( ntime_t t )
144{
145 return (t / NT_SECONDS_DIV) % NT_PERIOD_SECONDS;
146}
147
153double ntime_convertSeconds( ntime_t t )
154{
155 return ((double)t / (double)NT_SECONDS_DIV);
156}
157
161double ntime_getRemainder( ntime_t t )
162{
163 return (double)(t % NT_SECONDS_DIV);
164}
165
173char* ntime_pretty( ntime_t t, int d )
174{
175 char str[64];
176 ntime_prettyBuf( str, sizeof(str), t, d );
177 return strdup(str);
178}
179
188void ntime_prettyBuf( char *str, int max, ntime_t t, int d )
189{
190 ntime_t nt;
191 int cycles, periods, seconds;
192
193 if (t==0)
194 nt = naev_time;
195 else
196 nt = t;
197
198 /* UST (Universal Synchronized Time) - unit is seconds */
199 cycles = ntime_getCycles( nt );
200 periods = ntime_getPeriods( nt );
201 seconds = ntime_getSeconds( nt );
202 if ((cycles == 0) && (periods == 0)) /* only seconds */
203 snprintf( str, max, _("%04d s"), seconds );
204 else if ((cycles == 0) || (d==0))
205 snprintf( str, max, _("%.*f p"), d, periods + 0.0001 * seconds );
206 else /* UST format */
207 snprintf( str, max, _("UST %d:%.*f"), cycles, d, periods + 0.0001 * seconds );
208}
209
215void ntime_set( ntime_t t )
216{
217 naev_time = t;
218 naev_remainder = 0.;
219}
220
224void ntime_setR( int cycles, int periods, int seconds, double rem )
225{
226 naev_time = ntime_create( cycles, periods, seconds );
227 naev_time += floor(rem);
228 naev_remainder = fmod( rem, 1. );
229}
230
236void ntime_inc( ntime_t t )
237{
238 naev_time += t;
239 economy_update( t );
240
241 /* Run hooks. */
242 if (t > 0)
243 hooks_updateDate( t );
244}
245
251void ntime_allowUpdate( int enable )
252{
253 ntime_enable = enable;
254}
255
264void ntime_incLagged( ntime_t t )
265{
266 NTimeUpdate_t *ntu, *iter;
267
268 /* Create the time increment. */
269 ntu = malloc(sizeof(NTimeUpdate_t));
270 ntu->next = NULL;
271 ntu->inc = t;
272
273 /* Only member. */
274 if (ntime_inclist == NULL)
275 ntime_inclist = ntu;
276
277 else {
278 /* Find end of list. */
279 for (iter = ntime_inclist; iter->next != NULL; iter = iter->next);
280 /* Append to end. */
281 iter->next = ntu;
282 }
283}
284
288void ntime_refresh (void)
289{
290 NTimeUpdate_t *ntu;
291
292 /* We have to run all the increments one by one to ensure all hooks get
293 * run and that no collisions occur. */
294 while (ntime_inclist != NULL) {
295 ntu = ntime_inclist;
296
297 /* Run hook stuff and actually update time. */
298 naev_time += ntu->inc;
299 economy_update( ntu->inc );
300
301 /* Remove the increment. */
302 ntime_inclist = ntu->next;
303
304 /* Free the increment. */
305 free(ntu);
306 }
307}
int economy_update(unsigned int dt)
Updates the economy.
Definition economy.c:516
void hooks_updateDate(ntime_t change)
Updates the time to see if it should be updated.
Definition hook.c:647
Header file with generic functions and naev-specifics.
void ntime_set(ntime_t t)
Sets the time absolutely, does NOT generate an event, used at init.
Definition ntime.c:215
ntime_t ntime_create(int scu, int stp, int stu)
Creates a time structure.
Definition ntime.c:94
static NTimeUpdate_t * ntime_inclist
Definition ntime.c:60
ntime_t ntime_get(void)
Gets the current time.
Definition ntime.c:108
char * ntime_pretty(ntime_t t, int d)
Gets the time in a pretty human readable format.
Definition ntime.c:173
static double naev_remainder
Definition ntime.c:63
int ntime_getSeconds(ntime_t t)
Gets the seconds of a time.
Definition ntime.c:143
void ntime_refresh(void)
Checks to see if ntime has any hooks pending to run.
Definition ntime.c:288
void ntime_inc(ntime_t t)
Sets the time relatively.
Definition ntime.c:236
void ntime_getR(int *cycles, int *periods, int *seconds, double *rem)
Gets the current time broken into individual components.
Definition ntime.c:116
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
void ntime_update(double dt)
Updatse the time based on realtime.
Definition ntime.c:69
void ntime_allowUpdate(int enable)
Allows the time to update when the game is updating.
Definition ntime.c:251
int ntime_getCycles(ntime_t t)
Gets the cycles of a time.
Definition ntime.c:127
void ntime_setR(int cycles, int periods, int seconds, double rem)
Loads time including remainder.
Definition ntime.c:224
int ntime_getPeriods(ntime_t t)
Gets the periods of a time.
Definition ntime.c:135
double ntime_getRemainder(ntime_t t)
Gets the remainder.
Definition ntime.c:161
static ntime_t naev_time
Definition ntime.c:62
double ntime_convertSeconds(ntime_t t)
Converts the time to seconds.
Definition ntime.c:153
void ntime_incLagged(ntime_t t)
Sets the time relatively.
Definition ntime.c:264
static const double d[]
Definition rng.c:273
Used for storing time increments to not trigger hooks during Lua calls and such.
Definition ntime.c:56
ntime_t inc
Definition ntime.c:58
struct NTimeUpdate_s * next
Definition ntime.c:57