diff --git a/src/SB/Core/x/xTimer.cpp b/src/SB/Core/x/xTimer.cpp index 938c1636..7d836d57 100644 --- a/src/SB/Core/x/xTimer.cpp +++ b/src/SB/Core/x/xTimer.cpp @@ -1,3 +1,82 @@ #include "xTimer.h" +#include "xMath.h" #include + +static U32 sPauseTimerHash[] = +{ + 0xBC345600, 0xBC345609, + 0xBC345683, 0xBC34568C, + 0xBC345706, 0xBC34570F, + 0xBC345789, 0xBC345792, + 0xBC34580C, 0xBC345815, + 0xBC34588F, 0xBC345898, + 0xBC345912, 0xBC34591B, + 0xBC345995, 0xBC34599E, + 0xBC345A18, 0xBC345A21, + 0xBC345A9B, 0xBC345AA4, +}; + +F32 GetRandomizedTime(xTimerAsset* tasset) +{ + U32 halfRangeMilli = 1000.0f * tasset->randomRange; + if (halfRangeMilli == 0) { + return tasset->seconds; + } + + S32 offset = xrand() % (halfRangeMilli * 2) - halfRangeMilli; + F32 time = tasset->seconds + offset / 1000.0f; + return time; +} + +void xTimerInit(void* b, void* tasset) +{ + xTimerInit((xBase*)b, (xTimerAsset*)tasset); +} + +S32 xTimer_ObjIDIsPauseTimer(U32 id) +{ + if (id == 0xCB3F6340) return TRUE; + if (id >= 0x016FC9F0 && id <= 0x016FC9F9) return TRUE; + + S32 foo = (id >= 0xBC345600); + S32 bar = (id <= 0xBC345AA4); + if (foo && bar) { + for (S32 i = 0; i < 10; i++) { + if (id >= sPauseTimerHash[i*2] && id <= sPauseTimerHash[i*2+1]) { + return TRUE; + } + } + } + + return FALSE; +} + +void xTimerInit(xBase* b, xTimerAsset* tasset) +{ + xBaseInit(b, tasset); + + xTimer* t = (xTimer*)b; + + t->eventFunc = xTimerEventCB; + t->tasset = tasset; + + if (t->linkCount) { + t->link = (xLinkAsset*)((U8*)t->tasset + sizeof(xTimerAsset)); + } else { + t->link = NULL; + } + + t->state = 0; + t->secondsLeft = GetRandomizedTime(tasset); + t->runsInPause = xTimer_ObjIDIsPauseTimer(b->id); + t->flags = 0; +} + +void xTimerReset(xTimer* ent) +{ + xBaseReset(ent, ent->tasset); + ent->state = 0; + ent->secondsLeft = GetRandomizedTime(ent->tasset); + ent->flags = 0; +} \ No newline at end of file diff --git a/src/SB/Core/x/xTimer.h b/src/SB/Core/x/xTimer.h index 20159580..51dc0865 100644 --- a/src/SB/Core/x/xTimer.h +++ b/src/SB/Core/x/xTimer.h @@ -21,8 +21,11 @@ struct xTimer : xBase struct xScene; void xTimerInit(void* b, void* tasset); +void xTimerInit(xBase* b, xTimerAsset* tasset); +void xTimerReset(xTimer* ent); void xTimerSave(xTimer* ent, xSerial* s); void xTimerLoad(xTimer* ent, xSerial* s); +S32 xTimerEventCB(xBase*, xBase* to, U32 toEvent, const F32* toParam, xBase*); void xTimerUpdate(xBase* to, xScene*, F32 dt); #endif