-
Notifications
You must be signed in to change notification settings - Fork 483
/
Copy pathuicommon.h
409 lines (340 loc) · 11 KB
/
uicommon.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#pragma once
#include <algorithm>
#include <cctype>
#include <functional>
#include <locale>
#include <map>
#include <string>
#include <set>
#include "MiscUtils.h"
#include <Console.h>
#include <Export.h>
#include <PluginManager.h>
#include <VTableInterpose.h>
#include "modules/Items.h"
#include "modules/Screen.h"
#include "modules/World.h"
#include "df/building_stockpilest.h"
#include "df/caravan_state.h"
#include "df/dfhack_material_category.h"
#include "df/enabler.h"
#include "df/item_quality.h"
#include "df/plotinfost.h"
#include "df/world.h"
using namespace std;
using std::string;
using std::vector;
using std::map;
using std::set;
using namespace DFHack;
using namespace df::enums;
#define COLOR_TITLE COLOR_BROWN
#define COLOR_UNSELECTED COLOR_GREY
#define COLOR_SELECTED COLOR_WHITE
#define COLOR_HIGHLIGHTED COLOR_GREEN
struct coord32_t
{
int32_t x, y, z;
coord32_t()
{
x = -30000;
y = -30000;
z = -30000;
}
coord32_t(df::coord& other)
{
x = other.x;
y = other.y;
z = other.z;
}
df::coord get_coord16() const
{
return df::coord(x, y, z);
}
};
typedef int8_t UIColor;
static inline void OutputString(UIColor color, int &x, int &y, const std::string &text,
bool newline = false, int left_margin = 0, const UIColor bg_color = 0, bool map = false)
{
Screen::paintString(Screen::Pen(' ', color, bg_color), x, y, text, map);
if (newline)
{
++y;
x = left_margin;
}
else
x += text.length();
}
static inline void OutputHotkeyString(int &x, int &y, const char *text, const char *hotkey, bool newline = false,
int left_margin = 0, int8_t text_color = COLOR_WHITE, int8_t hotkey_color = COLOR_LIGHTGREEN, bool map = false)
{
OutputString(hotkey_color, x, y, hotkey, false, 0, 0, map);
string display(": ");
display.append(text);
OutputString(text_color, x, y, display, newline, left_margin, 0, map);
}
static inline void OutputHotkeyString(int &x, int &y, const char *text, df::interface_key hotkey,
bool newline = false, int left_margin = 0, int8_t text_color = COLOR_WHITE, int8_t hotkey_color = COLOR_LIGHTGREEN,
bool map = false)
{
OutputHotkeyString(x, y, text, DFHack::Screen::getKeyDisplay(hotkey).c_str(), newline, left_margin, text_color, hotkey_color, map);
}
static inline void OutputLabelString(int &x, int &y, const char *text, const char *hotkey, const string &label, bool newline = false,
int left_margin = 0, int8_t text_color = COLOR_WHITE, int8_t hotkey_color = COLOR_LIGHTGREEN, bool map = false)
{
OutputString(hotkey_color, x, y, hotkey, false, 0, 0, map);
string display(": ");
display.append(text);
display.append(": ");
OutputString(text_color, x, y, display, false, 0, 0, map);
OutputString(hotkey_color, x, y, label, newline, left_margin, 0, map);
}
static inline void OutputLabelString(int &x, int &y, const char *text, df::interface_key hotkey, const string &label, bool newline = false,
int left_margin = 0, int8_t text_color = COLOR_WHITE, int8_t hotkey_color = COLOR_LIGHTGREEN, bool map = false)
{
OutputLabelString(x, y, text, DFHack::Screen::getKeyDisplay(hotkey).c_str(), label, newline,
left_margin, text_color, hotkey_color, map);
}
static inline void OutputFilterString(int &x, int &y, const char *text, const char *hotkey, bool state, bool newline = false,
int left_margin = 0, int8_t hotkey_color = COLOR_LIGHTGREEN, bool map = false)
{
OutputString(hotkey_color, x, y, hotkey, false, 0, 0, map);
OutputString(COLOR_WHITE, x, y, ": ", false, 0, 0, map);
OutputString((state) ? COLOR_WHITE : COLOR_GREY, x, y, text, newline, left_margin, 0, map);
}
static inline void OutputToggleString(int &x, int &y, const char *text, const char *hotkey, bool state, bool newline = true,
int left_margin = 0, int8_t color = COLOR_WHITE, int8_t hotkey_color = COLOR_LIGHTGREEN, bool map = false)
{
OutputHotkeyString(x, y, text, hotkey, false, 0, color, hotkey_color, map);
OutputString(color, x, y, ": ", false, 0, 0, map);
if (state)
OutputString(COLOR_GREEN, x, y, "On", newline, left_margin, 0, map);
else
OutputString(COLOR_GREY, x, y, "Off", newline, left_margin, 0, map);
}
static inline void OutputToggleString(int &x, int &y, const char *text, df::interface_key hotkey, bool state, bool newline = true,
int left_margin = 0, int8_t color = COLOR_WHITE, int8_t hotkey_color = COLOR_LIGHTGREEN, bool map = false)
{
OutputToggleString(x, y, text, DFHack::Screen::getKeyDisplay(hotkey).c_str(), state, newline, left_margin, color, hotkey_color, map);
}
static inline void set_to_limit(int &value, const int maximum, const int min = 0)
{
if (value < min)
value = min;
else if (value > maximum)
value = maximum;
}
inline void paint_text(const UIColor color, const int &x, const int &y, const std::string &text, const UIColor background = 0)
{
Screen::paintString(Screen::Pen(' ', color, background), x, y, text);
}
static inline string pad_string(string text, const int size, const bool front = true, const bool trim = false)
{
if (text.length() > size_t(size))
{
if (trim && size > 10)
{
text = text.substr(0, size-3);
text.append("...");
}
return text;
}
string aligned(size - text.length(), ' ');
if (front)
{
aligned.append(text);
return aligned;
}
else
{
text.append(aligned);
return text;
}
}
static inline df::interface_key get_string_key(const std::set<df::interface_key> *input)
{
for (auto it = input->begin(); it != input->end(); ++it)
{
if (DFHack::Screen::keyToChar(*it) >= 0)
return *it;
}
return df::interface_key::NONE;
}
static inline char get_string_input(const std::set<df::interface_key> *input)
{
return DFHack::Screen::keyToChar(get_string_key(input));
}
/*
* Utility Functions
*/
static inline bool can_trade()
{
if (df::global::plotinfo->caravans.size() == 0)
return false;
for (auto it = df::global::plotinfo->caravans.begin(); it != df::global::plotinfo->caravans.end(); it++)
{
typedef df::caravan_state::T_trade_state state;
auto caravan = *it;
auto trade_state = caravan->trade_state;
auto time_remaining = caravan->time_remaining;
if ((trade_state == state::Approaching || trade_state == state::AtDepot) && time_remaining != 0)
return true;
}
return false;
}
static inline bool is_metal_item(df::item *item)
{
MaterialInfo mat(item);
return (mat.getCraftClass() == craft_material_class::Metal);
}
static inline bool is_set_to_melt(df::item* item)
{
return item->flags.bits.melt;
}
// Copied from Kelly Martin's code
static inline bool can_melt(df::item* item)
{
df::item_flags bad_flags;
bad_flags.whole = 0;
#define F(x) bad_flags.bits.x = true;
F(dump); F(forbid); F(garbage_collect); F(in_job);
F(hostile); F(on_fire); F(rotten); F(trader);
F(in_building); F(construction); F(artifact); F(melt);
#undef F
if (item->flags.whole & bad_flags.whole)
return false;
df::item_type t = item->getType();
if (t == df::enums::item_type::BOX || t == df::enums::item_type::BAR)
return false;
if (!is_metal_item(item)) return false;
for (auto g = item->general_refs.begin(); g != item->general_refs.end(); g++)
{
switch ((*g)->getType())
{
case general_ref_type::CONTAINS_ITEM:
case general_ref_type::UNIT_HOLDER:
case general_ref_type::CONTAINS_UNIT:
return false;
case general_ref_type::CONTAINED_IN_ITEM:
{
df::item* c = (*g)->getItem();
for (auto gg = c->general_refs.begin(); gg != c->general_refs.end(); gg++)
{
if ((*gg)->getType() == general_ref_type::UNIT_HOLDER)
return false;
}
}
break;
default:
break;
}
}
if (item->getQuality() >= item_quality::Masterful)
return false;
return true;
}
/*
* Stockpile Access
*/
class StockpileInfo {
public:
StockpileInfo() : id(0), sp(nullptr), x1(-30000), x2(-30000), y1(-30000), y2(-30000), z(-30000)
{
}
StockpileInfo(df::building_stockpilest *sp_) : StockpileInfo()
{
sp = sp_;
readBuilding();
}
bool inStockpile(df::item *i)
{
// Check if the item is assigned to a stockpile
if (i->isAssignedToStockpile())
return i->isAssignedToThisStockpile(sp->id);
// If this is in a container check where the container item is
df::item* container = Items::getContainer(i);
if (container)
return inStockpile(container);
// Inventory items seems to have stale position information. We can
// assume that they are not in the stockpile.
if (i->flags.bits.in_inventory)
return false;
// Only containers are assigned to a stockpile. We need a coordinate
// check for free items.
if (i->pos.z != z) return false;
if (i->pos.x < x1 || i->pos.x >= x2 ||
i->pos.y < y1 || i->pos.y >= y2) return false;
int e = (i->pos.x - x1) + (i->pos.y - y1) * sp->room.width;
return sp->room.extents[e] == 1;
}
bool isValid()
{
if (!id)
return false;
auto found = df::building::find(id);
return found && found == sp && found->getType() == building_type::Stockpile;
}
int32_t getId()
{
return id;
}
bool matches(df::building_stockpilest* sp)
{
return this->sp == sp;
}
df::building_stockpilest* getStockpile()
{
return sp;
}
protected:
int32_t id;
df::building_stockpilest* sp;
void readBuilding()
{
if (!sp)
return;
id = sp->id;
z = sp->z;
x1 = sp->room.x;
x2 = sp->room.x + sp->room.width;
y1 = sp->room.y;
y2 = sp->room.y + sp->room.height;
}
protected:
int x1, x2, y1, y2, z;
};
class PersistentStockpileInfo : public StockpileInfo {
public:
PersistentStockpileInfo(df::building_stockpilest *sp, string persistence_key) :
StockpileInfo(sp), persistence_key(persistence_key)
{
}
PersistentStockpileInfo(PersistentDataItem &config, string persistence_key) :
config(config), persistence_key(persistence_key)
{
id = config.ival(1);
}
bool load()
{
auto found = df::building::find(id);
if (!found || found->getType() != building_type::Stockpile)
return false;
sp = virtual_cast<df::building_stockpilest>(found);
if (!sp)
return false;
readBuilding();
return true;
}
void save()
{
config = DFHack::World::AddPersistentData(persistence_key);
config.ival(1) = id;
}
void remove()
{
DFHack::World::DeletePersistentData(config);
}
protected:
PersistentDataItem config;
string persistence_key;
};