Lephisto/src/ltime.c
2014-05-27 20:48:00 +01:00

140 lines
3.2 KiB
C

/**
* @file ltime.c
*
* @brief Handle the lephisto time.
*
* The basic unit of time is the STU. There are 1000 STU in a MTU. The time
* representation is generally UST which consists of MTU.STU.
*
* Acronyms:
* - MTU : Major Time Unit (1000 STU)
* - STU : Synchronized Time Unit.
* - UST : Universal Synchonized Time.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "lephisto.h"
#include "hook.h"
#include "economy.h"
#include "ltime.h"
/**
* @brief Used for storing time increments to not trigger hooks during Lua
* calls and such.
*/
typedef struct LTimeUpdate_s {
struct LTimeUpdate_s* next; /**< Increment in the linked list. */
unsigned int inc; /**< Time increment associated. */
} LTimeUpdate_t;
static LTimeUpdate_t* ltime_inclist = NULL; /** Time increment list. */
static unsigned int lephisto_time = 0; /**< Contains the current time in mSTU. */
/**
* @brief Get the current time.
* @return The current time in mSTU.
*/
unsigned int ltime_get(void) {
return lephisto_time;
}
/**
* @brief Get the time in a pretty human readable format.
* @param t Time to print (in STU), if 0 it'll use the current time.
* @return The time in a human readable format (must free).
*/
char* ltime_pretty(unsigned int t) {
unsigned int lt;
int maj, stu;
char str[128], *ret;
if(t == 0) lt = lephisto_time;
else lt = t;
/* UST (Universal Synchronized Time) - unit is STU (Syncronized Time Unit). */
maj = lt / (1000*LTIME_UNIT_LENGTH);
stu = (lt / (LTIME_UNIT_LENGTH)) % 1000;
if(maj == 0) /* Only STU. */
snprintf(str, 128, "%03d STU", stu);
else /* Full format. */
snprintf(str, 128, "UST %d.%03d", maj, stu);
ret = strdup(str);
return ret;
}
/**
* @brief Set the time absolutely, does *not* generate an event, used at init.
* @param t Absolute time to set to in STU.
*/
void ltime_set(unsigned int t) {
lephisto_time = t;
}
/**
* @brief Set the time relatively.
* @param t Time modifier in STU.
*/
void ltime_inc(unsigned int t) {
lephisto_time += t;
hooks_run("time");
economy_update(t);
}
/**
* @brief Set the time relatively.
*
* This does *not* call hooks and such, they must be run with ltime_refresh
* manually later.
* @param t time modifier in STU.
*/
void ltime_incLagged(unsigned int t) {
LTimeUpdate_t* ltu, *iter;
/* Create the time increment. */
ltu = malloc(sizeof(LTimeUpdate_t));
ltu->next = NULL;
ltu->inc = t;
/* Only member. */
if(ltime_inclist == NULL)
ltime_inclist = ltu;
else {
/* Find end of list. */
for(iter = ltime_inclist; iter->next != NULL; iter = iter->next);
/* Append to end. */
iter->next = ltu;
}
}
/**
* @brief Check to see if ltime has any hooks pending to run.
*/
void ltime_refresh(void) {
LTimeUpdate_t* ltu;
/*
* We have to run all the increments one by one to ensure all hooks get
* run and that no collisions accur.
*/
while(ltime_inclist != NULL) {
ltu = ltime_inclist;
/* Run hook stuff and actually update time. */
lephisto_time += ltu->inc;
hooks_run("time");
economy_update(ltu->inc);
/* Remove the increment. */
ltime_inclist = ltu->next;
/* Free the increment. */
free(ltu);
}
}