-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathGeneralStructures.h
387 lines (298 loc) · 8.24 KB
/
GeneralStructures.h
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#pragma once
#include <ASMMacros.h>
#include <YRPPCore.h>
#include <YRMath.h>
#include <YRMathVector.h>
#include <GeneralDefinitions.h> // need eDirection for FacingStruct
#include <BasicStructures.h>
#include <string.h>
//used for cell coordinates/vectors
using CellStruct = Vector2D<short>;
using Point2D = Vector2D<int>;
using CoordStruct = Vector3D<int>;
// used for particle ColorList
using RGBClass = Vector3D<BYTE>; // <pd> wuhaha
//used for timed events, time measured in frames!
class TimerStruct
{
public:
int StartTime{ -1 };
int : 32;
int TimeLeft{ 0 };
constexpr TimerStruct() = default;
TimerStruct(int duration) { this->Start(duration); }
void Start(int duration) {
this->StartTime = Unsorted::CurrentFrame;
this->TimeLeft = duration;
}
void Stop() {
this->StartTime = -1;
this->TimeLeft = 0;
}
void Pause() {
if(this->IsTicking()) {
this->TimeLeft = this->GetTimeLeft();
this->StartTime = -1;
}
}
void Resume() {
if(!this->IsTicking()) {
this->StartTime = Unsorted::CurrentFrame;
}
}
int GetTimeLeft() const {
if(!this->IsTicking()) {
return this->TimeLeft;
}
auto passed = Unsorted::CurrentFrame - this->StartTime;
auto left = this->TimeLeft - passed;
return (left <= 0) ? 0 : left;
}
// returns whether a ticking timer has finished counting down.
bool Completed() const {
return this->IsTicking() && !this->HasTimeLeft();
}
// returns whether a delay is active or a timer is still counting down.
// this is the 'opposite' of Completed() (meaning: incomplete / still busy)
// and logically the same as !Expired() (meaning: blocked / delay in progress)
bool InProgress() const {
return this->IsTicking() && this->HasTimeLeft();
}
// returns whether a delay is inactive. same as !InProgress().
bool Expired() const {
return !this->IsTicking() || !this->HasTimeLeft();
}
protected:
bool IsTicking() const {
return this->StartTime != -1;
}
bool HasTimeLeft() const {
return this->GetTimeLeft() > 0;
}
};
class RepeatableTimerStruct : public TimerStruct
{
public:
int Duration{ 0 };
constexpr RepeatableTimerStruct() = default;
RepeatableTimerStruct(int duration) { this->Start(duration); }
void Start(int duration) {
this->Duration = duration;
this->Restart();
}
void Restart() {
this->TimerStruct::Start(this->Duration);
}
};
inline unsigned int TranslateFixedPoint(size_t bitsFrom, size_t bitsTo, unsigned int value, unsigned int offset = 0) {
const size_t MaskIn = ((1u << bitsFrom) - 1);
const size_t MaskOut = ((1u << bitsTo) - 1);
if(bitsFrom > bitsTo) {
// converting down
return (((((value & MaskIn) >> (bitsFrom - bitsTo - 1)) + 1) >> 1) + offset) & MaskOut;
} else if(bitsFrom < bitsTo) {
// converting up
return (((value - offset) & MaskIn) << (bitsTo - bitsFrom)) & MaskOut;
} else {
return value & MaskOut;
}
}
// like a compass with 2 ^ 16 units equalling 360°
struct DirStruct
{
using value_type = short;
using unsigned_type = unsigned short;
DirStruct() : DirStruct(0) { }
explicit DirStruct(int value) : Value(static_cast<value_type>(value)) { }
explicit DirStruct(double rad) : DirStruct() {
this->radians(rad);
}
DirStruct(size_t bits, value_type value)
: DirStruct(static_cast<value_type>(TranslateFixedPoint(bits, 16, static_cast<unsigned_type>(value), 0)))
{ }
bool operator == (const DirStruct& rhs) const {
return this->Value == rhs.Value;
}
bool operator != (const DirStruct& rhs) const {
return this->Value != rhs.Value;
}
DirStruct& operator += (const DirStruct& rhs) {
reinterpret_cast<unsigned_type&>(this->Value) += static_cast<unsigned_type>(rhs.value());
return *this;
}
DirStruct operator + (const DirStruct& rhs) const {
return DirStruct(*this) += rhs;
}
DirStruct& operator -= (const DirStruct& rhs) {
reinterpret_cast<unsigned_type&>(this->Value) -= static_cast<unsigned_type>(rhs.value());
return *this;
}
DirStruct operator - (const DirStruct& rhs) const {
return DirStruct(*this) -= rhs;
}
DirStruct operator - () const {
return DirStruct(-this->Value);
}
DirStruct operator + () const {
return *this;
}
template <size_t Bits>
value_type value() const {
static_assert(Bits > 0 && Bits <= 16, "Bits has to be greater than 0 and lower or equal to 16.");
return static_cast<value_type>(TranslateFixedPoint(16, Bits, static_cast<unsigned_type>(this->Value), 0));
}
template <>
value_type value<16>() const {
return this->Value;
}
template <size_t Bits>
void value(value_type value) {
static_assert(Bits > 0 && Bits <= 16, "Bits has to be greater than 0 and lower or equal to 16.");
this->Value = static_cast<value_type>(TranslateFixedPoint(Bits, 16, static_cast<unsigned_type>(value), 0));
}
value_type value8() const {
return this->value<3>();
}
void value8(value_type value) {
this->value<3>(value);
}
value_type value32() const {
return this->value<5>();
}
void value32(value_type value) {
this->value<5>(value);
}
value_type value256() const {
return this->value<8>();
}
void value256(value_type value) {
this->value<8>(value);
}
value_type value() const {
return this->value<16>();
}
void value(value_type value) {
this->value<16>(value);
}
template <size_t Bits = 16>
double radians() const {
static_assert(Bits > 0 && Bits <= 16, "Bits has to be greater than 0 and lower or equal to 16.");
static const int Max = ((1 << Bits) - 1);
int value = Max / 4 - this->value<Bits>();
return -value * -(Math::TwoPi / Max);
}
template <size_t Bits = 16>
void radians(double rad) {
static_assert(Bits > 0 && Bits <= 16, "Bits has to be greater than 0 and lower or equal to 16.");
static const int Max = ((1 << Bits) - 1);
int value = static_cast<int>(rad * (Max / Math::TwoPi));
this->value<Bits>(static_cast<value_type>(Max / 4 - value));
}
private:
value_type Value;
unsigned_type unused_2;
};
// managing a facing that can change over time
struct FacingStruct
{
FacingStruct() : Timer(0) { }
FacingStruct(short rot) : FacingStruct() {
this->turn_rate(rot);
}
short turn_rate() const {
return this->ROT.value();
}
void turn_rate(short value) {
if(value > 127) {
value = 127;
}
this->ROT.value<8>(value);
}
bool in_motion() const {
return this->turn_rate() > 0 && this->Timer.GetTimeLeft();
}
DirStruct target() const {
return this->Value;
}
DirStruct current() const {
auto ret = this->Value;
if(this->in_motion()) {
auto diff = this->difference();
auto num_steps = static_cast<short>(this->num_steps());
if(num_steps > 0) {
auto steps_left = this->Timer.GetTimeLeft();
ret.value(static_cast<short>(ret.value() - steps_left * diff / num_steps));
}
}
return ret;
}
bool set(const DirStruct& value) {
bool ret = (this->current() != value);
if(ret) {
this->Value = value;
this->Initial = value;
}
this->Timer.Start(0);
return ret;
}
bool turn(const DirStruct& value) {
if(this->Value == value) {
return false;
}
this->Initial = this->current();
this->Value = value;
if(this->turn_rate() > 0) {
this->Timer.Start(this->num_steps());
}
return true;
}
private:
short difference() const {
return static_cast<short>(this->Value.value() - this->Initial.value());
}
int num_steps() const {
return abs(this->difference()) / this->turn_rate();
}
DirStruct Value; // target facing
DirStruct Initial; // rotation started here
TimerStruct Timer; // counts rotation steps
DirStruct ROT; // Rate of Turn. INI Value * 256
};
struct SomeVoxelCache {
void *ptr;
DWORD f_4;
DWORD f_8;
BYTE f_C;
DWORD * ptr_10;
};
struct BasePlanningCell {
int Weight;
CellStruct Position;
};
// this crap is used in several Base planning routines
struct BasePlanningCellContainer {
BasePlanningCell * Items;
int Count;
int Capacity;
bool Sorted;
DWORD Unknown_10;
bool AddCapacity(int AdditionalCapacity)
{ JMP_THIS(0x510860); }
// for qsort
static int __cdecl Comparator(const void *, const void *)
{ JMP_STD(0x5108F0); }
};
// combines number and a string
struct NamedValue {
const char* Name;
int Value;
bool operator == (int value) const {
return this->Value == value;
}
bool operator == (const char* name) const {
return !_strcmpi(this->Name, name);
}
bool operator == (const NamedValue& other) const {
return this->Value == other.Value && *this == other.Name;
}
};