[Add] Added the possibility to do lagged time increments (for when

adding to mission subsystems).
This commit is contained in:
Allanis 2014-05-27 20:48:00 +01:00
parent 30a0f3c7b7
commit 74e1ba3342
2 changed files with 66 additions and 0 deletions

View File

@ -13,12 +13,23 @@
*/
#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. */
/**
@ -74,4 +85,55 @@ void ltime_inc(unsigned int t) {
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);
}
}

View File

@ -10,4 +10,8 @@ char* ltime_pretty(unsigned int t);
/* Set. */
void ltime_set(unsigned int t);
void ltime_inc(unsigned int t);
void ltime_incLagged(unsigned int t);
/* Misc. */
void ltime_refresh(void);