2012-04-21 05:43:52 -06:00
|
|
|
#include "Core.h"
|
|
|
|
#include "Console.h"
|
|
|
|
#include "Export.h"
|
|
|
|
#include "PluginManager.h"
|
|
|
|
|
|
|
|
#include "modules/Gui.h"
|
|
|
|
#include "modules/Translation.h"
|
|
|
|
#include "modules/Units.h"
|
2012-05-21 12:29:03 -06:00
|
|
|
#include "modules/Job.h"
|
2012-04-21 05:43:52 -06:00
|
|
|
|
|
|
|
#include "LuaTools.h"
|
|
|
|
|
|
|
|
#include "DataDefs.h"
|
|
|
|
#include "df/ui.h"
|
|
|
|
#include "df/world.h"
|
|
|
|
#include "df/viewscreen_joblistst.h"
|
|
|
|
#include "df/viewscreen_unitlistst.h"
|
2012-04-22 09:22:00 -06:00
|
|
|
#include "df/viewscreen_layer_militaryst.h"
|
|
|
|
#include "df/viewscreen_layer_workshop_profilest.h"
|
|
|
|
#include "df/viewscreen_layer_noblelistst.h"
|
|
|
|
#include "df/viewscreen_layer_overall_healthst.h"
|
2012-05-18 09:18:49 -06:00
|
|
|
#include "df/viewscreen_layer_assigntradest.h"
|
|
|
|
#include "df/viewscreen_tradegoodsst.h"
|
2012-04-21 05:43:52 -06:00
|
|
|
#include "df/viewscreen_dwarfmodest.h"
|
2012-04-22 09:22:00 -06:00
|
|
|
#include "df/viewscreen_petst.h"
|
2012-05-21 12:29:03 -06:00
|
|
|
#include "df/viewscreen_storesst.h"
|
2012-04-22 09:22:00 -06:00
|
|
|
#include "df/layer_object_listst.h"
|
2012-05-18 09:18:49 -06:00
|
|
|
#include "df/assign_trade_status.h"
|
2012-04-21 05:43:52 -06:00
|
|
|
|
|
|
|
#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;
|
2012-04-25 08:28:00 -06:00
|
|
|
using df::global::ui_building_in_assign;
|
2012-04-22 09:22:00 -06:00
|
|
|
using df::global::ui_building_item_cursor;
|
2012-04-25 08:28:00 -06:00
|
|
|
using df::global::ui_building_assign_type;
|
|
|
|
using df::global::ui_building_assign_is_marked;
|
|
|
|
using df::global::ui_building_assign_units;
|
|
|
|
using df::global::ui_building_assign_items;
|
2012-04-21 05:43:52 -06:00
|
|
|
|
|
|
|
static bool unit_list_hotkey(df::viewscreen *top);
|
2012-05-18 09:18:49 -06:00
|
|
|
static bool item_list_hotkey(df::viewscreen *top);
|
2012-04-21 05:43:52 -06:00
|
|
|
|
|
|
|
static command_result sort_units(color_ostream &out, vector <string> & parameters);
|
2012-05-18 09:18:49 -06:00
|
|
|
static command_result sort_items(color_ostream &out, vector <string> & parameters);
|
2012-04-21 05:43:52 -06:00
|
|
|
|
|
|
|
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,
|
2012-04-21 06:53:17 -06:00
|
|
|
" sort-units order [order...]\n"
|
2012-04-21 05:43:52 -06:00
|
|
|
" Sort the unit list using the given sequence of comparisons.\n"
|
2012-04-21 06:53:17 -06:00
|
|
|
" 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"
|
2012-04-21 05:43:52 -06:00
|
|
|
));
|
2012-05-18 09:18:49 -06:00
|
|
|
commands.push_back(PluginCommand(
|
|
|
|
"sort-items", "Sort the visible item list.", sort_items, item_list_hotkey,
|
|
|
|
" sort-items order [order...]\n"
|
|
|
|
" Sort the item 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"
|
|
|
|
" Item order examples:\n"
|
|
|
|
" description, material, wear, type, quality\n"
|
|
|
|
"The orderings are defined in hack/lua/plugins/sort/*.lua\n"
|
|
|
|
));
|
2012-04-21 05:43:52 -06:00
|
|
|
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]];
|
|
|
|
}
|
|
|
|
|
2012-04-22 09:22:00 -06:00
|
|
|
template<class T>
|
|
|
|
void reorder_cursor(T *cursor, const std::vector<unsigned> &order)
|
|
|
|
{
|
|
|
|
if (*cursor == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < order.size(); i++)
|
|
|
|
{
|
|
|
|
if (unsigned(*cursor) == order[i])
|
|
|
|
{
|
|
|
|
*cursor = T(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-21 05:43:52 -06:00
|
|
|
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;
|
|
|
|
|
2012-04-22 09:22:00 -06:00
|
|
|
Lua::StackUnwinder frame(L, 1);
|
|
|
|
|
2012-04-21 05:43:52 -06:00
|
|
|
if (!lua_istable(L, -1))
|
|
|
|
{
|
|
|
|
out.printerr("Not a table returned as ordering.\n");
|
2012-04-22 09:22:00 -06:00
|
|
|
return false;
|
2012-04-21 05:43:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lua_rawlen(L, -1) != size)
|
|
|
|
{
|
|
|
|
out.printerr("Invalid ordering size: expected %d, actual %d\n", size, lua_rawlen(L, -1));
|
2012-04-22 09:22:00 -06:00
|
|
|
return false;
|
2012-04-21 05:43:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
order->clear();
|
|
|
|
order->resize(size);
|
|
|
|
found.resize(size);
|
|
|
|
|
|
|
|
for (size_t i = 1; i <= size; i++)
|
|
|
|
{
|
2012-04-22 09:22:00 -06:00
|
|
|
lua_rawgeti(L, frame[1], i);
|
2012-04-21 05:43:52 -06:00
|
|
|
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);
|
2012-04-22 09:22:00 -06:00
|
|
|
return false;
|
2012-04-21 05:43:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (found[v-1])
|
|
|
|
{
|
|
|
|
out.printerr("Duplicate order value: %d\n", v);
|
2012-04-22 09:22:00 -06:00
|
|
|
return false;
|
2012-04-21 05:43:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
found[v-1] = 1;
|
|
|
|
(*order)[i-1] = v-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2012-04-22 09:22:00 -06:00
|
|
|
Lua::PushVector(L, key, true);
|
2012-04-21 05:43:52 -06:00
|
|
|
lua_pushvalue(L, base+2);
|
|
|
|
|
|
|
|
if (!Lua::SafeCall(out, L, 2, 1))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return read_order(out, L, order, key.size());
|
|
|
|
}
|
|
|
|
|
2012-04-22 09:22:00 -06:00
|
|
|
static bool ParseSpec(color_ostream &out, lua_State *L, const char *type, vector<string> ¶ms)
|
|
|
|
{
|
2012-05-18 09:18:49 -06:00
|
|
|
if (!parse_ordering_spec(out, L, type, params))
|
2012-04-22 09:22:00 -06:00
|
|
|
{
|
|
|
|
out.printerr("Invalid ordering specification for %s.\n", type);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PARSE_SPEC(type, params) \
|
2012-05-19 09:50:36 -06:00
|
|
|
std::vector<unsigned> order; \
|
|
|
|
if (!ParseSpec(*pout, L, type, params)) return;
|
2012-04-21 05:43:52 -06:00
|
|
|
|
2012-05-18 09:18:49 -06:00
|
|
|
static bool prepare_sort(color_ostream *pout, lua_State *L)
|
|
|
|
{
|
|
|
|
if (L)
|
|
|
|
{
|
|
|
|
if (!Lua::PushModulePublic(*pout, L, "plugins.sort", "make_sort_order"))
|
|
|
|
{
|
|
|
|
pout->printerr("Cannot access the sorter function.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-22 09:22:00 -06:00
|
|
|
static void sort_null_first(vector<string> ¶meters)
|
2012-04-21 05:43:52 -06:00
|
|
|
{
|
2012-04-22 09:22:00 -06:00
|
|
|
vector_insert_at(parameters, 0, std::string("<exists"));
|
|
|
|
}
|
2012-04-21 05:43:52 -06:00
|
|
|
|
2012-04-22 09:22:00 -06:00
|
|
|
static df::layer_object_listst *getLayerList(df::viewscreen_layerst *layer, int idx)
|
|
|
|
{
|
|
|
|
return virtual_cast<df::layer_object_listst>(vector_get(layer->layer_objects,idx));
|
|
|
|
}
|
2012-04-21 05:43:52 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
typedef void (*SortHandler)(color_ostream *pout, lua_State *L, int top,
|
|
|
|
df::viewscreen *screen, vector<string> ¶meters);
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
#define VIEWSCREEN(name) df::viewscreen_##name##st
|
|
|
|
#define DEFINE_SORT_HANDLER(map, screen_type, tail, screen) \
|
|
|
|
static void CONCAT_TOKENS(SortHandler_##screen_type,__LINE__)\
|
|
|
|
(color_ostream *pout, lua_State *L, int top, \
|
|
|
|
VIEWSCREEN(screen_type) *screen, vector<string> ¶meters); \
|
|
|
|
DFHACK_STATIC_ADD_TO_MAP(&map, #screen_type tail, \
|
|
|
|
(SortHandler)CONCAT_TOKENS(SortHandler_##screen_type,__LINE__) ); \
|
|
|
|
static void CONCAT_TOKENS(SortHandler_##screen_type,__LINE__)\
|
|
|
|
(color_ostream *pout, lua_State *L, int top, \
|
|
|
|
VIEWSCREEN(screen_type) *screen, vector<string> ¶meters)
|
2012-04-21 05:43:52 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
static std::map<std::string, SortHandler> unit_sorters;
|
2012-04-21 05:43:52 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
/*
|
|
|
|
* Sort units in the 'u'nit list screen.
|
|
|
|
*/
|
2012-04-21 05:43:52 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
DEFINE_SORT_HANDLER(unit_sorters, unitlist, "", units)
|
|
|
|
{
|
|
|
|
PARSE_SPEC("units", parameters);
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
int page = units->page;
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
if (compute_order(*pout, L, top, &order, units->units[page]))
|
|
|
|
{
|
|
|
|
reorder_cursor(&units->cursor_pos[page], order);
|
|
|
|
reorder_vector(&units->units[page], order);
|
|
|
|
reorder_vector(&units->jobs[page], order);
|
|
|
|
}
|
|
|
|
}
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
/*
|
|
|
|
* Sort units in the 'j'ob list screen.
|
|
|
|
*/
|
2012-04-21 05:43:52 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
DEFINE_SORT_HANDLER(unit_sorters, joblist, "", jobs)
|
|
|
|
{
|
|
|
|
PARSE_SPEC("units", parameters);
|
|
|
|
|
2012-05-21 12:29:03 -06:00
|
|
|
std::vector<df::unit*> units;
|
|
|
|
for (size_t i = 0; i < jobs->units.size(); i++)
|
|
|
|
{
|
|
|
|
auto unit = jobs->units[i];
|
|
|
|
if (!unit && jobs->jobs[i])
|
|
|
|
unit = Job::getWorker(jobs->jobs[i]);
|
|
|
|
units.push_back(unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (compute_order(*pout, L, top, &order, units))
|
2012-04-21 05:43:52 -06:00
|
|
|
{
|
2012-05-19 09:50:36 -06:00
|
|
|
reorder_cursor(&jobs->cursor_pos, order);
|
|
|
|
reorder_vector(&jobs->units, order);
|
|
|
|
reorder_vector(&jobs->jobs, order);
|
|
|
|
}
|
|
|
|
}
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
/*
|
|
|
|
* Sort candidate units in the 'p'osition page of the 'm'ilitary screen.
|
|
|
|
*/
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
DEFINE_SORT_HANDLER(unit_sorters, layer_military, "/Positions/Candidates", military)
|
|
|
|
{
|
|
|
|
auto &candidates = military->positions.candidates;
|
|
|
|
auto list3 = getLayerList(military, 2);
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
PARSE_SPEC("units", parameters);
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
if (compute_order(*pout, L, top, &order, candidates))
|
2012-04-22 09:22:00 -06:00
|
|
|
{
|
2012-05-19 09:50:36 -06:00
|
|
|
reorder_cursor(&list3->cursor, order);
|
|
|
|
reorder_vector(&candidates, order);
|
|
|
|
}
|
|
|
|
}
|
2012-04-22 09:22:00 -06:00
|
|
|
|
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
/*
|
|
|
|
* Sort units in the workshop 'q'uery 'P'rofile modification screen.
|
|
|
|
*/
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
DEFINE_SORT_HANDLER(unit_sorters, layer_workshop_profile, "/Unit", profile)
|
|
|
|
{
|
|
|
|
auto list1 = getLayerList(profile, 0);
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
PARSE_SPEC("units", parameters);
|
2012-04-21 05:43:52 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
if (compute_order(*pout, L, top, &order, profile->workers))
|
|
|
|
{
|
|
|
|
reorder_cursor(&list1->cursor, order);
|
|
|
|
reorder_vector(&profile->workers, order);
|
|
|
|
}
|
|
|
|
}
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
DEFINE_SORT_HANDLER(unit_sorters, layer_noblelist, "/Appoint", nobles)
|
|
|
|
{
|
|
|
|
auto list2 = getLayerList(nobles, 1);
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
sort_null_first(parameters);
|
|
|
|
PARSE_SPEC("units", parameters);
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
std::vector<df::unit*> units;
|
|
|
|
for (size_t i = 0; i < nobles->candidates.size(); i++)
|
|
|
|
units.push_back(nobles->candidates[i]->unit);
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
if (compute_order(*pout, L, top, &order, units))
|
|
|
|
{
|
|
|
|
reorder_cursor(&list2->cursor, order);
|
|
|
|
reorder_vector(&nobles->candidates, order);
|
|
|
|
}
|
|
|
|
}
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
/*
|
|
|
|
* Sort animal units in the Animal page of the 'z' status screen.
|
|
|
|
*/
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
DEFINE_SORT_HANDLER(unit_sorters, pet, "/List", animals)
|
|
|
|
{
|
|
|
|
PARSE_SPEC("units", parameters);
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
std::vector<df::unit*> units;
|
|
|
|
for (size_t i = 0; i < animals->animal.size(); i++)
|
|
|
|
units.push_back(animals->is_vermin[i] ? NULL : (df::unit*)animals->animal[i]);
|
2012-04-21 05:43:52 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
if (compute_order(*pout, L, top, &order, units))
|
|
|
|
{
|
|
|
|
reorder_cursor(&animals->cursor, order);
|
|
|
|
reorder_vector(&animals->animal, order);
|
|
|
|
reorder_vector(&animals->is_vermin, order);
|
|
|
|
reorder_vector(&animals->pet_info, order);
|
|
|
|
reorder_vector(&animals->is_tame, order);
|
|
|
|
reorder_vector(&animals->is_adopting, order);
|
|
|
|
}
|
|
|
|
}
|
2012-04-21 05:43:52 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
/*
|
|
|
|
* Sort candidate trainers in the Animal page of the 'z' status screen.
|
|
|
|
*/
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
DEFINE_SORT_HANDLER(unit_sorters, pet, "/SelectTrainer", animals)
|
|
|
|
{
|
|
|
|
sort_null_first(parameters);
|
|
|
|
PARSE_SPEC("units", parameters);
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
if (compute_order(*pout, L, top, &order, animals->trainer_unit))
|
|
|
|
{
|
|
|
|
reorder_cursor(&animals->trainer_cursor, order);
|
|
|
|
reorder_vector(&animals->trainer_unit, order);
|
|
|
|
reorder_vector(&animals->trainer_mode, order);
|
|
|
|
}
|
|
|
|
}
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
/*
|
|
|
|
* Sort units in the Health page of the 'z' status screen.
|
|
|
|
*/
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
DEFINE_SORT_HANDLER(unit_sorters, layer_overall_health, "/Units", health)
|
|
|
|
{
|
|
|
|
auto list1 = getLayerList(health, 0);
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
PARSE_SPEC("units", parameters);
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
if (compute_order(*pout, L, top, &order, health->unit))
|
2012-04-21 05:43:52 -06:00
|
|
|
{
|
2012-05-19 09:50:36 -06:00
|
|
|
reorder_cursor(&list1->cursor, order);
|
|
|
|
reorder_vector(&health->unit, order);
|
|
|
|
reorder_vector(&health->bits1, order);
|
|
|
|
reorder_vector(&health->bits2, order);
|
|
|
|
reorder_vector(&health->bits3, order);
|
2012-04-21 05:43:52 -06:00
|
|
|
}
|
2012-05-19 09:50:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sort burrow member candidate units in the 'w' sidebar mode.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEFINE_SORT_HANDLER(unit_sorters, dwarfmode, "/Burrows/AddUnits", screen)
|
|
|
|
{
|
|
|
|
PARSE_SPEC("units", parameters);
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
if (compute_order(*pout, L, top, &order, ui->burrows.list_units))
|
|
|
|
{
|
|
|
|
reorder_cursor(&ui->burrows.unit_cursor_pos, order);
|
|
|
|
reorder_vector(&ui->burrows.list_units, order);
|
|
|
|
reorder_vector(&ui->burrows.sel_units, order);
|
|
|
|
}
|
|
|
|
}
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
/*
|
|
|
|
* Sort building owner candidate units in the 'q' sidebar mode, or cage assignment.
|
|
|
|
*/
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
DEFINE_SORT_HANDLER(unit_sorters, dwarfmode, "/QueryBuilding/Some/Assign", screen)
|
|
|
|
{
|
|
|
|
sort_null_first(parameters);
|
2012-04-22 09:22:00 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
PARSE_SPEC("units", parameters);
|
|
|
|
if (compute_order(*pout, L, top, &order, *ui_building_assign_units))
|
2012-04-21 05:43:52 -06:00
|
|
|
{
|
2012-05-19 09:50:36 -06:00
|
|
|
reorder_cursor(ui_building_item_cursor, order);
|
|
|
|
reorder_vector(ui_building_assign_type, order);
|
|
|
|
reorder_vector(ui_building_assign_units, order);
|
2012-08-03 08:09:51 -06:00
|
|
|
// this is for cages. cages need some extra sorting
|
|
|
|
if(ui_building_assign_items->size() == ui_building_assign_units->size())
|
|
|
|
{
|
|
|
|
reorder_vector(ui_building_assign_items, order);
|
|
|
|
reorder_vector(ui_building_assign_is_marked, order);
|
|
|
|
}
|
2012-05-19 09:50:36 -06:00
|
|
|
}
|
|
|
|
}
|
2012-04-21 05:43:52 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
/*
|
|
|
|
* Sort pen assignment candidate units in 'z'->'N'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEFINE_SORT_HANDLER(unit_sorters, dwarfmode, "/ZonesPenInfo/Assign", screen)
|
|
|
|
{
|
|
|
|
PARSE_SPEC("units", parameters);
|
|
|
|
|
|
|
|
if (compute_order(*pout, L, top, &order, *ui_building_assign_units))
|
|
|
|
{
|
|
|
|
reorder_cursor(ui_building_item_cursor, order);
|
|
|
|
reorder_vector(ui_building_assign_type, order);
|
|
|
|
reorder_vector(ui_building_assign_units, order);
|
|
|
|
reorder_vector(ui_building_assign_items, order);
|
|
|
|
reorder_vector(ui_building_assign_is_marked, order);
|
2012-04-21 05:43:52 -06:00
|
|
|
}
|
2012-04-22 09:22:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool unit_list_hotkey(df::viewscreen *screen)
|
|
|
|
{
|
2012-05-19 09:50:36 -06:00
|
|
|
auto focus = Gui::getFocusString(screen);
|
|
|
|
return findPrefixInMap(unit_sorters, focus) != NULL;
|
2012-04-22 09:22:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static command_result sort_units(color_ostream &out, vector <string> ¶meters)
|
|
|
|
{
|
|
|
|
if (parameters.empty())
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
|
|
|
auto L = Lua::Core::State;
|
|
|
|
auto screen = Core::getInstance().getTopViewscreen();
|
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
Lua::StackUnwinder top(L);
|
|
|
|
|
|
|
|
if (!prepare_sort(&out, L))
|
2012-04-22 09:22:00 -06:00
|
|
|
return CR_WRONG_USAGE;
|
2012-04-21 05:43:52 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
auto focus = Gui::getFocusString(screen);
|
|
|
|
auto handler = findPrefixInMap(unit_sorters, focus);
|
|
|
|
|
|
|
|
if (!handler)
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
else
|
|
|
|
handler(&out, L, top, screen, parameters);
|
|
|
|
|
2012-04-21 05:43:52 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
2012-05-18 09:18:49 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
static std::map<std::string, SortHandler> item_sorters;
|
2012-05-18 09:18:49 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
DEFINE_SORT_HANDLER(item_sorters, tradegoods, "/Items/Broker", trade)
|
|
|
|
{
|
|
|
|
PARSE_SPEC("items", parameters);
|
2012-05-18 09:18:49 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
if (compute_order(*pout, L, top, &order, trade->broker_items))
|
2012-05-18 09:18:49 -06:00
|
|
|
{
|
2012-05-19 09:50:36 -06:00
|
|
|
reorder_cursor(&trade->broker_cursor, order);
|
|
|
|
reorder_vector(&trade->broker_items, order);
|
|
|
|
reorder_vector(&trade->broker_selected, order);
|
|
|
|
reorder_vector(&trade->broker_count, order);
|
2012-05-18 09:18:49 -06:00
|
|
|
}
|
2012-05-19 09:50:36 -06:00
|
|
|
}
|
2012-05-18 09:18:49 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
DEFINE_SORT_HANDLER(item_sorters, tradegoods, "/Items/Trader", trade)
|
|
|
|
{
|
|
|
|
PARSE_SPEC("items", parameters);
|
2012-05-18 09:18:49 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
if (compute_order(*pout, L, top, &order, trade->trader_items))
|
|
|
|
{
|
|
|
|
reorder_cursor(&trade->trader_cursor, order);
|
|
|
|
reorder_vector(&trade->trader_items, order);
|
|
|
|
reorder_vector(&trade->trader_selected, order);
|
|
|
|
reorder_vector(&trade->trader_count, order);
|
|
|
|
}
|
|
|
|
}
|
2012-05-18 09:18:49 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
DEFINE_SORT_HANDLER(item_sorters, layer_assigntrade, "/Items", bring)
|
|
|
|
{
|
|
|
|
auto list1 = getLayerList(bring, 0);
|
|
|
|
auto list2 = getLayerList(bring, 1);
|
|
|
|
int list_idx = vector_get(bring->visible_lists, list1->cursor, (int16_t)-1);
|
2012-05-18 09:18:49 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
PARSE_SPEC("items", parameters);
|
2012-05-18 09:18:49 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
auto &vec = bring->lists[list_idx];
|
2012-05-18 09:18:49 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
std::vector<df::item*> items;
|
|
|
|
for (size_t i = 0; i < vec.size(); i++)
|
|
|
|
items.push_back(bring->info[vec[i]]->item);
|
2012-05-18 09:18:49 -06:00
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
if (compute_order(*pout, L, top, &order, items))
|
|
|
|
{
|
|
|
|
reorder_cursor(&list2->cursor, order);
|
|
|
|
reorder_vector(&vec, order);
|
2012-05-18 09:18:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-21 12:29:03 -06:00
|
|
|
DEFINE_SORT_HANDLER(item_sorters, stores, "/Items", stocks)
|
|
|
|
{
|
|
|
|
PARSE_SPEC("items", parameters);
|
|
|
|
|
|
|
|
if (compute_order(*pout, L, top, &order, stocks->items))
|
|
|
|
{
|
|
|
|
reorder_cursor(&stocks->item_cursor, order);
|
|
|
|
reorder_vector(&stocks->items, order);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-18 09:18:49 -06:00
|
|
|
static bool item_list_hotkey(df::viewscreen *screen)
|
|
|
|
{
|
2012-05-19 09:50:36 -06:00
|
|
|
auto focus = Gui::getFocusString(screen);
|
|
|
|
return findPrefixInMap(item_sorters, focus) != NULL;
|
2012-05-18 09:18:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static command_result sort_items(color_ostream &out, vector <string> ¶meters)
|
|
|
|
{
|
|
|
|
if (parameters.empty())
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
|
|
|
auto L = Lua::Core::State;
|
|
|
|
auto screen = Core::getInstance().getTopViewscreen();
|
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
Lua::StackUnwinder top(L);
|
|
|
|
|
|
|
|
if (!prepare_sort(&out, L))
|
2012-05-18 09:18:49 -06:00
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
2012-05-19 09:50:36 -06:00
|
|
|
auto focus = Gui::getFocusString(screen);
|
|
|
|
auto handler = findPrefixInMap(item_sorters, focus);
|
|
|
|
|
|
|
|
if (!handler)
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
else
|
|
|
|
handler(&out, L, top, screen, parameters);
|
|
|
|
|
2012-05-18 09:18:49 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|