Skip to content

Commit 389fe6a

Browse files
committed
Add QueueClass, replace EventList and EventClass::AddEvent
1 parent 0c6a17b commit 389fe6a

File tree

2 files changed

+148
-30
lines changed

2 files changed

+148
-30
lines changed

EventClass.h

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,32 @@
11
#pragma once
22

33
#include <ScenarioClass.h>
4+
#include <QueueClass.h>
45
#include <TargetClass.h>
56
#include <Unsorted.h>
67

78
#pragma pack(push, 1)
8-
9-
class EventClass;
10-
11-
template<size_t Length>
12-
struct EventList
13-
{
14-
public:
15-
int Count;
16-
int Head;
17-
int Tail;
18-
EventClass List[Length];
19-
int Timings[Length];
20-
};
21-
229
class EventClass
2310
{
2411
public:
2512
DEFINE_ARRAY_REFERENCE(const char*, [47], EventNames, 0x0082091C)
2613

27-
DEFINE_REFERENCE(EventList<0x80>, OutList, 0x00A802C8)
28-
DEFINE_REFERENCE(EventList<0x4000>, DoList, 0x008B41F8)
14+
enum { MAX_EVENTS = 128 };
2915

30-
// If the event is a MegaMission, then add it to this list
31-
DEFINE_REFERENCE(EventList<0x100>, MegaMissionList, 0x00A83ED0)
16+
DEFINE_REFERENCE((QueueClass<EventClass, MAX_EVENTS>), OutList, 0x00A802C8)
17+
DEFINE_REFERENCE((QueueClass<EventClass, MAX_EVENTS * 128>), DoList, 0x008B41F8)
18+
19+
// Seems to be some queue used internally, not added to from outside Execute_DoList
20+
//DEFINE_REFERENCE((QueueClass<EventClass, MAX_EVENTS * 2>), SomeList, 0x00A83ED0)
3221

3322
// this points to CRCs from 0x100 last frames
3423
DEFINE_ARRAY_REFERENCE(DWORD, [256], LatestFramesCRC, 0x00B04474)
3524
DEFINE_REFERENCE(DWORD, CurrentFrameCRC, 0x00AC51FC)
3625

26+
[[deprecated("Use OutList.Add() instead.")]]
3727
static bool AddEvent(const EventClass& event)
3828
{
39-
if (OutList.Count >= 128)
40-
return false;
41-
42-
OutList.List[OutList.Tail] = event;
43-
44-
#pragma warning(suppress: 4996)
45-
OutList.Timings[OutList.Tail] = static_cast<int>(Imports::TimeGetTime()());
46-
47-
++OutList.Count;
48-
OutList.Tail = (LOBYTE(OutList.Tail) + 1) & 127;
49-
50-
return true;
29+
return OutList.Add(event);
5130
}
5231

5332
explicit EventClass(int houseIndex, EventType eventType)

QueueClass.h

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*******************************************************************************
2+
/* O P E N S O U R C E -- T S + + **
3+
/*******************************************************************************
4+
*
5+
* @project TS++
6+
*
7+
* @file QUEUE.H
8+
*
9+
* @authors CCHyper, ZivDero (adjustments for YR)
10+
*
11+
* @brief FIFO queue for events.
12+
*
13+
* @license TS++ is free software: you can redistribute it and/or
14+
* modify it under the terms of the GNU General Public License
15+
* as published by the Free Software Foundation, either version
16+
* 3 of the License, or (at your option) any later version.
17+
*
18+
* TS++ is distributed in the hope that it will be
19+
* useful, but WITHOUT ANY WARRANTY; without even the implied
20+
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
21+
* PURPOSE. See the GNU General Public License for more details.
22+
*
23+
* You should have received a copy of the GNU General Public
24+
* License along with this program.
25+
* If not, see <http://www.gnu.org/licenses/>.
26+
*
27+
******************************************************************************/
28+
#pragma once
29+
30+
#include <Unsorted.h>
31+
32+
33+
template<class T, int size>
34+
class QueueClass
35+
{
36+
public:
37+
QueueClass();
38+
~QueueClass() { }
39+
40+
T& operator[](int index);
41+
42+
T& First();
43+
void Init();
44+
int Next();
45+
bool Add(const T& q);
46+
47+
int GetHead();
48+
int GetTail();
49+
T* GetArray();
50+
51+
public:
52+
int Count;
53+
54+
private:
55+
int Head;
56+
int Tail;
57+
T Array[size];
58+
int Timings[size];
59+
};
60+
61+
62+
template<class T, int size>
63+
inline QueueClass<T, size>::QueueClass() :
64+
Count(0)
65+
{
66+
Init();
67+
}
68+
69+
70+
template<class T, int size>
71+
inline void QueueClass<T, size>::Init()
72+
{
73+
Count = 0;
74+
Head = 0;
75+
Tail = 0;
76+
}
77+
78+
79+
template<class T, int size>
80+
inline bool QueueClass<T, size>::Add(const T& q)
81+
{
82+
if (Count < size)
83+
{
84+
Array[Tail] = q;
85+
#pragma warning(suppress: 4996)
86+
Timings[Tail] = static_cast<int>(Imports::TimeGetTime()());
87+
Tail = (Tail + 1) & (size - 1);
88+
Count = Count + 1;
89+
return true;
90+
}
91+
return false;
92+
}
93+
94+
95+
template<class T, int size>
96+
inline int QueueClass<T, size>::Next()
97+
{
98+
if (Count)
99+
{
100+
Head = (Head + 1) & (size - 1);
101+
Count = Count - 1;
102+
}
103+
return Count;
104+
}
105+
106+
107+
template<class T, int size>
108+
inline T& QueueClass<T, size>::operator[](int index)
109+
{
110+
return Array[(Head + index) & (size - 1)];
111+
}
112+
113+
114+
template<class T, int size>
115+
inline T& QueueClass<T, size>::First()
116+
{
117+
return Array[Head];
118+
}
119+
120+
121+
template<class T, int size>
122+
inline int QueueClass<T, size>::GetHead()
123+
{
124+
return Head;
125+
}
126+
127+
128+
template<class T, int size>
129+
inline int QueueClass<T, size>::GetTail()
130+
{
131+
return Tail;
132+
}
133+
134+
135+
template<class T, int size>
136+
inline T* QueueClass<T, size>::GetArray()
137+
{
138+
return Array;
139+
}

0 commit comments

Comments
 (0)