forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.cpp
More file actions
240 lines (195 loc) · 5.95 KB
/
Copy pathsort.cpp
File metadata and controls
240 lines (195 loc) · 5.95 KB
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
#include "Core.h"
#include "Console.h"
#include "Export.h"
#include "PluginManager.h"
#include "modules/Gui.h"
#include "modules/Translation.h"
#include "modules/Units.h"
#include "LuaTools.h"
#include "DataDefs.h"
#include "df/ui.h"
#include "df/world.h"
#include "df/viewscreen_joblistst.h"
#include "df/viewscreen_unitlistst.h"
#include "df/viewscreen_dwarfmodest.h"
#include "MiscUtils.h"
#include <stdlib.h>
using std::vector;
using std::string;
using std::endl;
using namespace DFHack;
using namespace df::enums;
using df::global::ui;
using df::global::world;
static bool unit_list_hotkey(df::viewscreen *top);
static command_result sort_units(color_ostream &out, vector <string> & parameters);
DFHACK_PLUGIN("sort");
DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands)
{
commands.push_back(PluginCommand(
"sort-units", "Sort the visible unit list.", sort_units, unit_list_hotkey,
" sort-units order [order...]\n"
" Sort the unit list using the given sequence of comparisons.\n"
" The '<' prefix for an order makes undefined values sort first.\n"
" The '>' prefix reverses the sort order for defined values.\n"
" Unit order examples:\n"
" name, age, arrival, squad, squad_position, profession\n"
"The orderings are defined in hack/lua/plugins/sort/*.lua\n"
));
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
return CR_OK;
}
template<class T>
void reorder_vector(std::vector<T> *pvec, const std::vector<unsigned> &order)
{
assert(pvec->size() == order.size());
std::vector<T> tmp(*pvec);
for (size_t i = 0; i < order.size(); i++)
(*pvec)[i] = tmp[order[i]];
}
bool parse_ordering_spec(color_ostream &out, lua_State *L, std::string type, const std::vector<std::string> ¶ms)
{
if (!lua_checkstack(L, params.size() + 2))
return false;
if (!Lua::PushModulePublic(out, L, "plugins.sort", "parse_ordering_spec"))
return false;
Lua::Push(L, type);
for (size_t i = 0; i < params.size(); i++)
Lua::Push(L, params[i]);
if (!Lua::SafeCall(out, L, params.size()+1, 1))
return false;
if (!lua_istable(L, -1))
{
lua_pop(L, 1);
return false;
}
return true;
}
bool read_order(color_ostream &out, lua_State *L, std::vector<unsigned> *order, size_t size)
{
std::vector<char> found;
if (!lua_istable(L, -1))
{
out.printerr("Not a table returned as ordering.\n");
goto fail;
}
if (lua_rawlen(L, -1) != size)
{
out.printerr("Invalid ordering size: expected %d, actual %d\n", size, lua_rawlen(L, -1));
goto fail;
}
order->clear();
order->resize(size);
found.resize(size);
for (size_t i = 1; i <= size; i++)
{
lua_rawgeti(L, -1, i);
int v = lua_tointeger(L, -1);
lua_pop(L, 1);
if (v < 1 || size_t(v) > size)
{
out.printerr("Order value out of range: %d\n", v);
goto fail;
}
if (found[v-1])
{
out.printerr("Duplicate order value: %d\n", v);
goto fail;
}
found[v-1] = 1;
(*order)[i-1] = v-1;
}
lua_pop(L, 1);
return true;
fail:
lua_pop(L, 1);
return false;
}
template<class T>
bool compute_order(color_ostream &out, lua_State *L, int base, std::vector<unsigned> *order, const std::vector<T> &key)
{
lua_pushvalue(L, base+1);
Lua::PushVector(L, key);
lua_pushvalue(L, base+2);
if (!Lua::SafeCall(out, L, 2, 1))
return false;
return read_order(out, L, order, key.size());
}
static bool unit_list_hotkey(df::viewscreen *screen)
{
if (strict_virtual_cast<df::viewscreen_unitlistst>(screen))
return true;
if (strict_virtual_cast<df::viewscreen_joblistst>(screen))
return true;
if (strict_virtual_cast<df::viewscreen_dwarfmodest>(screen))
{
using namespace df::enums::ui_sidebar_mode;
switch (ui->main.mode)
{
case Burrows:
return ui->burrows.in_add_units_mode;
default:
return false;
}
}
return false;
}
static command_result sort_units(color_ostream &out, vector <string> ¶meters)
{
if (parameters.empty())
return CR_WRONG_USAGE;
auto L = Lua::Core::State;
int top = lua_gettop(L);
if (!Lua::Core::PushModulePublic(out, "plugins.sort", "make_sort_order"))
{
out.printerr("Cannot access the sorter function.\n");
return CR_WRONG_USAGE;
}
if (!parse_ordering_spec(out, L, "units", parameters))
{
out.printerr("Invalid unit ordering specification.\n");
lua_settop(L, top);
return CR_WRONG_USAGE;
}
auto screen = Core::getInstance().getTopViewscreen();
std::vector<unsigned> order;
if (auto units = strict_virtual_cast<df::viewscreen_unitlistst>(screen))
{
for (int i = 0; i < 4; i++)
{
if (compute_order(out, L, top, &order, units->units[i]))
{
reorder_vector(&units->units[i], order);
reorder_vector(&units->jobs[i], order);
}
}
}
else if (auto jobs = strict_virtual_cast<df::viewscreen_joblistst>(screen))
{
if (compute_order(out, L, top, &order, jobs->units))
{
reorder_vector(&jobs->units, order);
reorder_vector(&jobs->jobs, order);
}
}
else if (strict_virtual_cast<df::viewscreen_dwarfmodest>(screen))
{
switch (ui->main.mode)
{
case ui_sidebar_mode::Burrows:
if (compute_order(out, L, top, &order, ui->burrows.list_units))
{
reorder_vector(&ui->burrows.list_units, order);
reorder_vector(&ui->burrows.sel_units, order);
}
break;
default:
break;;
}
}
lua_settop(L, top);
return CR_OK;
}