summaryrefslogtreecommitdiff
path: root/src/eventlooper.cpp
blob: 6bc0526eaac8497520bfd782ddbebbba6a7f6e56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <common.h>
#include <game.h>

struct EventLooper {
	u32 id;			// 0x00
	u32 settings;	// 0x04
	u16 name;		// 0x08
	u8 _0A[6];		// 0x0A
	u8 _10[0x9C];	// 0x10
	float x;		// 0xAC
	float y;		// 0xB0
	float z;		// 0xB4
	u8 _B8[0x318];	// 0xB8
	// Any variables you add to the class go here; starting at offset 0x3D0
	u64 eventFlag;	// 0x3D0
	u64 eventActive;	// 0x3D0
	u8 delay;		// 0x3D4
	u8 delayCount;	// 0x3D7
};

struct EventTable_t {
	u64 events;
	// ...
};

extern EventTable_t *EventTable;

void EventLooper_Update(EventLooper *self);



bool EventLooper_Create(EventLooper *self) {
	char eventStart	= (self->settings >> 24)	& 0xFF;
	char eventEnd	= (self->settings >> 16)	& 0xFF;

	// Putting all the events into the flag
	int i;
	u64 q = (u64)0;
	for(i=eventStart;i<(eventEnd+1);i++)
	{
		q = q | ((u64)1 << (i - 1));
	}
		
	self->eventFlag = q;
	
	self->delay		= (((self->settings) & 0xFF) + 1) * 10;
	self->delayCount = 0;
	
	char tmpEvent= (self->settings >> 8)	& 0xFF;
	if (tmpEvent == 0)
	{
		self->eventActive = (u64)0xFFFFFFFFFFFFFFFF;
	}
	else
	{
		self->eventActive = (u64)1 << (tmpEvent - 1);
		
	}
	

	if (EventTable->events & self->eventActive)
	{
		u64 evState = (u64)1 << (eventStart - 1);
		EventTable->events = EventTable->events | evState;
	}

	EventLooper_Update(self);
	
	return true;
}

bool EventLooper_Execute(EventLooper *self) {
	EventLooper_Update(self);
	return true;
}


void EventLooper_Update(EventLooper *self) {
	
	if ((EventTable->events & self->eventActive) == 0)
		return;

	// Waiting for the right moment
	if (self->delayCount < self->delay) 
	{
//		OSReport("Delaying: %d / %d\n", self->delayCount, self->delay);

		self->delayCount = self->delayCount + 1;
		return;
	}	
	
	// Reset the delay
	self->delayCount = 0;
	
	// Find which event(s) is/are on
	u64 evState = EventTable->events & self->eventFlag;
	
	// Turn off the old events
	EventTable->events = EventTable->events & (~self->eventFlag);
	
	// Shift them right if they can, if not, reset!
	evState = evState << 1;
	if (evState < self->eventFlag)
	{
		EventTable->events = EventTable->events | evState;
	}
	
	else
	{
		char eventStart	= (self->settings >> 24)	& 0xFF;
		evState = (u64)1 << (eventStart - 1);
		EventTable->events = EventTable->events | evState;
	}
	
	
}