Skip to content

Commit 5cae965

Browse files
authored
Merge pull request #5646 from ab9rf/sort-fixes
update `sort` for changes in df `unit_list`
2 parents dca7d8d + 04cd971 commit 5cae965

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

library/include/DataIdentity.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ namespace df
840840
template<class T>
841841
inline const container_identity* identity_traits<std::unordered_set<T> >::get()
842842
{
843-
using container = std::set<T>;
843+
using container = std::unordered_set<T>;
844844
static const ro_stl_container_identity<container> identity("unordered_set", identity_traits<T>::get());
845845
return &identity;
846846
}

plugins/sort.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <variant>
2+
13
#include "Debug.h"
24
#include "LuaTools.h"
35
#include "PluginManager.h"
@@ -25,7 +27,7 @@ namespace DFHack {
2527
DBG_DECLARE(sort, log, DebugCategory::LINFO);
2628
}
2729

28-
using item_or_unit = std::pair<void *, bool>;
30+
using item_or_unit = std::variant<df::unit*, df::item*>;
2931
using filter_vec_type = std::vector<std::function<bool(item_or_unit)>>;
3032

3133
// recreated here since our autogenerated df::sort_entry lacks template params
@@ -44,8 +46,8 @@ static bool probing = false;
4446
static bool probe_result = false;
4547

4648
static bool do_filter(const char *module_name, const char *fn_name, const item_or_unit &elem) {
47-
if (elem.second) return true;
48-
auto unit = (df::unit *)elem.first;
49+
if (std::holds_alternative<df::item*>(elem)) return true;
50+
auto unit = std::get<df::unit*>(elem);
4951

5052
if (probing) {
5153
TRACE(log).print("probe successful\n");
@@ -78,11 +80,11 @@ static bool do_work_animal_assignment_filter(item_or_unit elem) {
7880

7981
static int32_t our_filter_idx(df::widget_unit_list *unitlist) {
8082
if (world->units.active.empty())
81-
return true;
83+
return -1;
8284

8385
df::unit *u = world->units.active[0]; // any unit will do; we just need a sentinel
8486
if (!u)
85-
return true;
87+
return -1;
8688

8789
probing = true;
8890
probe_result = false;
@@ -91,8 +93,8 @@ static int32_t our_filter_idx(df::widget_unit_list *unitlist) {
9193
filter_vec_type *filter_vec = reinterpret_cast<filter_vec_type *>(&unitlist->filter_func);
9294

9395
TRACE(log).print("probing for our filter function\n");
94-
for (auto fn : *filter_vec) {
95-
fn(std::make_pair(u, false));
96+
for (auto& fn : *filter_vec) {
97+
fn(item_or_unit(u));
9698
if (probe_result) {
9799
TRACE(log).print("found our filter function at idx %d\n", idx);
98100
break;
@@ -144,13 +146,13 @@ static df::widget_unit_list * get_work_animal_assignment_unit_list() {
144146
//
145147

146148
static bool sort_proxy(const item_or_unit &a, const item_or_unit &b) {
147-
if (a.second || b.second)
149+
if (std::holds_alternative<df::item*>(a) || std::holds_alternative<df::item*>(b))
148150
return true;
149151

150152
bool ret = true;
151153
color_ostream &out = Core::getInstance().getConsole();
152154
Lua::CallLuaModuleFunction(out, "plugins.sort", "do_sort",
153-
std::make_tuple((df::unit *)a.first, (df::unit *)b.first),
155+
std::make_tuple(std::get<df::unit*>(a), std::get<df::unit*>(b)),
154156
1, [&](lua_State *L){
155157
ret = lua_toboolean(L, 1);
156158
}

0 commit comments

Comments
 (0)