forked from DFHack/stonesense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommonTypes.h
284 lines (259 loc) · 6.11 KB
/
commonTypes.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
#pragma once
#include <filesystem>
#include <unordered_map>
#include <concepts>
#include "common.h"
#include "SpriteColors.h"
#include "df/enabler.h"
#include "df/graphic.h"
enum ShadeBy {
ShadeNone,
ShadeXml,
ShadeNamed,
ShadeMat,
ShadeLayer,
ShadeVein,
ShadeMatFore,
ShadeMatBack,
ShadeLayerFore,
ShadeLayerBack,
ShadeVeinFore,
ShadeVeinBack,
ShadeBodyPart,
ShadeJob,
ShadeBlood,
ShadeBuilding,
ShadeGrass,
ShadeItem,
ShadeEquip,
ShadeWood,
ShadeGrowth
} ;
enum hairstyles {
hairstyles_invalid = -1,
NEATLY_COMBED,
BRAIDED,
DOUBLE_BRAID,
PONY_TAILS,
CLEAN_SHAVEN,
hairstyles_end
};
enum hairtypes {
hairtypes_invalid = -1,
HAIR,
BEARD,
MOUSTACHE,
SIDEBURNS,
hairtypes_end
};
struct t_subSprite {
int32_t sheetIndex;
int32_t fileIndex;
ALLEGRO_COLOR shadeColor;
ShadeBy shadeBy;
char bodyPart[128];
uint8_t snowMin;
uint8_t snowMax;
} ;
struct t_SpriteWithOffset {
int32_t sheetIndex;
int16_t x;
int16_t y;
int32_t fileIndex;
uint8_t numVariations;
char animFrames;
ALLEGRO_COLOR shadeColor;
bool needOutline;
std::vector<t_subSprite> subSprites;
ShadeBy shadeBy;
char bodyPart[128];
uint8_t snowMin;
uint8_t snowMax;
} ;
struct Crd2D {
int32_t x,y;
};
struct Crd3D {
int32_t x,y,z;
constexpr Crd3D operator+(const Crd3D rhs)
{
return Crd3D{ x + rhs.x, y + rhs.y, z + rhs.z };
}
};
class dfColors
{
public:
dfColors() {
memset(colors, 0, sizeof(colors));
update();
}
struct color {
uint8_t red;
uint8_t green;
uint8_t blue;
ALLEGRO_COLOR al;
void update() {
al = al_map_rgb(red,green,blue);
}
} colors[16]{
{ 0,0,0 }, // black
{ 13,103,196 }, // blue
{ 68,158,53 }, // green
{ 86,163,205 }, // cyan
{ 151,26,26 }, // red
{ 255,110,187 }, // magenta
{ 120,94,47 }, // brown
{ 185,192,162 }, // light gray
{ 88,83,86 }, // dark gray
{ 145,202,255 }, // light blue
{ 131,212,82 }, // light green
{ 176,223,215 }, // light cyan
{ 255,34,34 }, // light red
{ 255,167,246 }, // light magenta
{ 255,218,90 }, // yellow
{ 255,255,255 } // white
};
enum color_name {
black,
blue,
green,
cyan,
red,
magenta,
brown,
lgray,
dgray,
lblue,
lgreen,
lcyan,
lred,
lmagenta,
yellow,
white
};
void update() {
for (int i = 0; i < 16; i++) {
colors[i].update();
}
}
color & operator [] (color_name col) {
return colors[col];
}
ALLEGRO_COLOR getDfColor(int color, bool useDfColors) const {
if(color < 0 || color >= 16) {
return al_map_rgb(255,255,255);
}
if (useDfColors)
{
return al_map_rgb_f(df::global::gps->ccolor[color][0], df::global::gps->ccolor[color][1], df::global::gps->ccolor[color][2]);
}
return colors[ (color_name) color].al;
}
ALLEGRO_COLOR getDfColor(int color, int bright, bool useDfColors) const {
return getDfColor(color + (bright * 8), useDfColors);
}
};
// this is required because gcc 10 can't handle a dependently typed non-type template argu,ent
#if defined(__GNUC__) && __GNUC__ < 11
template <std::floating_point T>
#else
template <std::floating_point T, T alpha = T{ 0.9 } >
#endif
class RollingAverage
{
private:
#if defined(__GNUC__) && __GNUC__ < 11
static constexpr T alpha = T{ 0.9 };
#endif
T store;
bool empty{ true };
public:
const T get() const { return store; }
void update(const T val) { store = empty ? val : store * alpha + val * (T{ 1.0 } - alpha); empty = false; }
operator T() const { return store; }
};
struct FrameTimers{
RollingAverage<float> read_time;
RollingAverage<float> beautify_time;
RollingAverage<float> assembly_time;
RollingAverage<float> draw_time;
RollingAverage<float> overlay_time;
RollingAverage<float> frame_total;
clock_t prev_frame_time{ clock() };
};
struct SS_Item {
DFHack::t_matglossPair item;
DFHack::t_matglossPair matt;
DFHack::t_matglossPair dyematt;
};
struct worn_item {
DFHack::t_matglossPair matt;
DFHack::t_matglossPair dyematt;
int8_t rating;
worn_item();
};
struct unit_inventory {
//[item_type][item_subtype][item_number]
std::vector<std::vector<std::vector<worn_item>>> item;
};
struct Stonesense_Unit{
df::unit * origin;
uint16_t profession;
std::string custom_profession;
int32_t squad_leader_id;
uint32_t nbcolors;
uint32_t color[15]; // Was using DFHack::Units::MAX_COLORS for no apparent reason; TODO: Use a better number?
hairstyles hairstyle[hairtypes_end];
uint32_t hairlength[hairtypes_end];
bool isLegend;
std::unique_ptr<unit_inventory> inv;
};
struct Stonesense_Building
{
uint32_t x1;
uint32_t y1;
uint32_t x2;
uint32_t y2;
uint32_t z;
DFHack::t_matglossPair material;
df::building_type type;
union
{
int16_t subtype;
df::civzone_type civzone_type;
df::furnace_type furnace_type;
df::workshop_type workshop_type;
df::construction_type construction_type;
df::shop_type shop_type;
df::siegeengine_type siegeengine_type;
df::trap_type trap_type;
};
int32_t custom_type;
df::building* origin;
};
template <typename Key, typename Val, typename Hash = std::hash<Key>>
class SparseArray {
std::unordered_map< Key, Val, Hash> map;
public:
void clear()
{
map.clear();
}
void add(Key&& k, Val& v)
{
auto it = map.find(k);
if (it != map.end())
{
it->second = v;
}
else
{
map.emplace(k, v);
}
}
std::optional<Val> lookup(Key&& k)
{
auto it = map.find(k);
return it != map.end() ? std::optional{ it->second } : std::nullopt;
}
};