From 74e1ba3342a34028d3ecde1277a9a93295115c29 Mon Sep 17 00:00:00 2001 From: Allanis Date: Tue, 27 May 2014 20:48:00 +0100 Subject: [PATCH] [Add] Added the possibility to do lagged time increments (for when adding to mission subsystems). --- src/ltime.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ltime.h | 4 ++++ 2 files changed, 66 insertions(+) diff --git a/src/ltime.c b/src/ltime.c index 7e5cd38..5050abc 100644 --- a/src/ltime.c +++ b/src/ltime.c @@ -13,12 +13,23 @@ */ #include #include +#include #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); + } +} diff --git a/src/ltime.h b/src/ltime.h index f436da2..32e7304 100644 --- a/src/ltime.h +++ b/src/ltime.h @@ -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);