2012-01-07 10:47:23 -07:00
|
|
|
#include "Core.h"
|
2012-01-15 13:54:14 -07:00
|
|
|
#include "Console.h"
|
|
|
|
#include "Export.h"
|
|
|
|
#include "PluginManager.h"
|
|
|
|
#include "MiscUtils.h"
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-01-15 13:54:14 -07:00
|
|
|
#include "modules/Materials.h"
|
|
|
|
#include "modules/Items.h"
|
|
|
|
#include "modules/Gui.h"
|
|
|
|
#include "modules/Job.h"
|
|
|
|
#include "modules/World.h"
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-01-15 13:54:14 -07:00
|
|
|
#include "DataDefs.h"
|
|
|
|
#include "df/world.h"
|
|
|
|
#include "df/ui.h"
|
|
|
|
#include "df/building_workshopst.h"
|
|
|
|
#include "df/building_furnacest.h"
|
|
|
|
#include "df/job.h"
|
|
|
|
#include "df/job_item.h"
|
|
|
|
#include "df/job_list_link.h"
|
|
|
|
#include "df/dfhack_material_category.h"
|
|
|
|
#include "df/item.h"
|
2012-08-06 08:55:55 -06:00
|
|
|
#include "df/item_quality.h"
|
2012-01-15 13:54:14 -07:00
|
|
|
#include "df/items_other_id.h"
|
|
|
|
#include "df/tool_uses.h"
|
|
|
|
#include "df/general_ref.h"
|
|
|
|
#include "df/general_ref_unit_workerst.h"
|
|
|
|
#include "df/general_ref_unit_holderst.h"
|
|
|
|
#include "df/general_ref_building_holderst.h"
|
|
|
|
#include "df/general_ref_contains_itemst.h"
|
|
|
|
#include "df/general_ref_contained_in_itemst.h"
|
|
|
|
#include "df/general_ref_contains_unitst.h"
|
|
|
|
#include "df/itemdef_foodst.h"
|
|
|
|
#include "df/reaction.h"
|
|
|
|
#include "df/reaction_reagent_itemst.h"
|
|
|
|
#include "df/reaction_product_itemst.h"
|
|
|
|
#include "df/plant_raw.h"
|
|
|
|
#include "df/inorganic_raw.h"
|
|
|
|
#include "df/builtin_mats.h"
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
#include "df/viewscreen_dwarfmodest.h"
|
|
|
|
#include "df/itemdef_weaponst.h"
|
|
|
|
#include "df/itemdef_trapcompst.h"
|
|
|
|
#include "df/itemdef_toyst.h"
|
|
|
|
#include "df/itemdef_toolst.h"
|
|
|
|
#include "df/itemdef_instrumentst.h"
|
|
|
|
#include "df/itemdef_armorst.h"
|
|
|
|
#include "df/itemdef_ammost.h"
|
|
|
|
#include "df/itemdef_siegeammost.h"
|
|
|
|
#include "df/itemdef_glovesst.h"
|
|
|
|
#include "df/itemdef_shoesst.h"
|
|
|
|
#include "df/itemdef_shieldst.h"
|
|
|
|
#include "df/itemdef_helmst.h"
|
|
|
|
#include "df/itemdef_pantsst.h"
|
|
|
|
#include "df/itemdef_foodst.h"
|
|
|
|
#include "df/trapcomp_flags.h"
|
|
|
|
|
|
|
|
#include <deque>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <VTableInterpose.h>
|
|
|
|
#include <modules/Screen.h>
|
|
|
|
#include "df/creature_raw.h"
|
2012-11-05 11:48:24 -07:00
|
|
|
#include "df/enabler.h"
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
using std::deque;
|
2012-11-05 11:48:24 -07:00
|
|
|
using df::global::gps;
|
|
|
|
using df::global::enabler;
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
|
2012-01-07 10:47:23 -07:00
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
|
|
|
using std::endl;
|
|
|
|
using namespace DFHack;
|
|
|
|
using namespace df::enums;
|
|
|
|
|
|
|
|
using df::global::world;
|
|
|
|
using df::global::ui;
|
|
|
|
using df::global::ui_workshop_job_cursor;
|
2012-10-24 05:09:34 -06:00
|
|
|
using df::global::ui_workshop_in_add;
|
2012-01-07 10:47:23 -07:00
|
|
|
using df::global::job_next_id;
|
|
|
|
|
|
|
|
/* Plugin registration */
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static command_result workflow_cmd(color_ostream &out, vector <string> & parameters);
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void init_state(color_ostream &out);
|
|
|
|
static void cleanup_state(color_ostream &out);
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-02-21 10:19:17 -07:00
|
|
|
DFHACK_PLUGIN("workflow");
|
2012-01-07 10:47:23 -07:00
|
|
|
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
void OutputString(int8_t color, int &x, int &y, const std::string &text, bool newline = false, int left_margin = 0)
|
|
|
|
{
|
|
|
|
Screen::paintString(Screen::Pen(' ', color, 0), x, y, text);
|
|
|
|
if (newline)
|
|
|
|
{
|
|
|
|
++y;
|
|
|
|
x = left_margin;
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
2012-10-24 05:09:34 -06:00
|
|
|
else
|
|
|
|
x += text.length();
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
void OutputHotkeyString(int &x, int &y, const char *text, const char *hotkey, bool newline = false, int left_margin = 0)
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
2012-10-24 05:09:34 -06:00
|
|
|
OutputString(10, x, y, hotkey);
|
|
|
|
string display(": ");
|
|
|
|
display.append(text);
|
|
|
|
OutputString(15, x, y, display, newline, left_margin);
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
/******************************
|
|
|
|
* JOB STATE TRACKING STRUCTS *
|
|
|
|
******************************/
|
|
|
|
|
|
|
|
const int DAY_TICKS = 1200;
|
|
|
|
const int MONTH_DAYS = 28;
|
|
|
|
const int YEAR_MONTHS = 12;
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-01-09 08:20:28 -07:00
|
|
|
struct ItemConstraint;
|
|
|
|
|
2012-01-07 10:47:23 -07:00
|
|
|
struct ProtectedJob {
|
|
|
|
int id;
|
|
|
|
int building_id;
|
2012-01-11 09:04:04 -07:00
|
|
|
int tick_idx;
|
|
|
|
|
|
|
|
static int cur_tick_idx;
|
2012-01-07 10:47:23 -07:00
|
|
|
|
|
|
|
df::building *holder;
|
|
|
|
df::job *job_copy;
|
2012-01-10 06:23:37 -07:00
|
|
|
int reaction_id;
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-01-09 08:20:28 -07:00
|
|
|
df::job *actual_job;
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
bool want_resumed;
|
|
|
|
int resume_time, resume_delay;
|
|
|
|
|
2012-01-09 08:20:28 -07:00
|
|
|
std::vector<ItemConstraint*> constraints;
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
public:
|
|
|
|
ProtectedJob(df::job *job) : id(job->id)
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
2012-01-11 09:04:04 -07:00
|
|
|
tick_idx = cur_tick_idx;
|
2012-04-10 01:43:36 -06:00
|
|
|
holder = Job::getHolder(job);
|
2012-01-07 10:47:23 -07:00
|
|
|
building_id = holder ? holder->id : -1;
|
2012-04-10 01:43:36 -06:00
|
|
|
job_copy = Job::cloneJobStruct(job);
|
2012-01-09 08:20:28 -07:00
|
|
|
actual_job = job;
|
2012-01-10 06:23:37 -07:00
|
|
|
reaction_id = -1;
|
2012-01-11 09:04:04 -07:00
|
|
|
|
|
|
|
want_resumed = false;
|
|
|
|
resume_time = 0; resume_delay = DAY_TICKS;
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
~ProtectedJob()
|
|
|
|
{
|
2012-04-10 01:43:36 -06:00
|
|
|
Job::deleteJobStruct(job_copy);
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
bool isActuallyResumed() {
|
|
|
|
return actual_job && !actual_job->flags.bits.suspend;
|
|
|
|
}
|
|
|
|
bool isResumed() {
|
|
|
|
return want_resumed || isActuallyResumed();
|
|
|
|
}
|
|
|
|
bool isLive() { return actual_job != NULL; }
|
|
|
|
|
2012-01-07 10:47:23 -07:00
|
|
|
void update(df::job *job)
|
|
|
|
{
|
2012-01-09 08:20:28 -07:00
|
|
|
actual_job = job;
|
2012-01-07 10:47:23 -07:00
|
|
|
if (*job == *job_copy)
|
|
|
|
return;
|
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
reaction_id = -1;
|
2012-04-10 01:43:36 -06:00
|
|
|
Job::deleteJobStruct(job_copy);
|
|
|
|
job_copy = Job::cloneJobStruct(job);
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
2012-01-11 09:04:04 -07:00
|
|
|
|
|
|
|
void tick_job(df::job *job, int ticks)
|
|
|
|
{
|
|
|
|
tick_idx = cur_tick_idx;
|
|
|
|
actual_job = job;
|
|
|
|
|
|
|
|
if (isActuallyResumed())
|
|
|
|
{
|
|
|
|
resume_time = 0;
|
|
|
|
resume_delay = std::max(DAY_TICKS, resume_delay - ticks);
|
|
|
|
}
|
|
|
|
else if (want_resumed)
|
|
|
|
{
|
|
|
|
if (!resume_time)
|
|
|
|
want_resumed = false;
|
|
|
|
else if (world->frame_counter >= resume_time)
|
|
|
|
actual_job->flags.bits.suspend = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void recover(df::job *job)
|
|
|
|
{
|
|
|
|
actual_job = job;
|
|
|
|
job->flags.bits.repeat = true;
|
|
|
|
job->flags.bits.suspend = true;
|
|
|
|
|
|
|
|
resume_delay = std::min(DAY_TICKS*MONTH_DAYS, 5*resume_delay/3);
|
|
|
|
resume_time = world->frame_counter + resume_delay;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_resumed(bool resume)
|
|
|
|
{
|
|
|
|
if (resume)
|
|
|
|
{
|
|
|
|
if (world->frame_counter >= resume_time)
|
|
|
|
actual_job->flags.bits.suspend = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-12 09:07:53 -07:00
|
|
|
resume_time = 0;
|
2012-01-11 09:04:04 -07:00
|
|
|
if (isActuallyResumed())
|
|
|
|
resume_delay = DAY_TICKS;
|
|
|
|
|
|
|
|
actual_job->flags.bits.suspend = true;
|
|
|
|
}
|
2012-01-12 09:07:53 -07:00
|
|
|
|
|
|
|
want_resumed = resume;
|
2012-01-11 09:04:04 -07:00
|
|
|
}
|
2012-01-07 10:47:23 -07:00
|
|
|
};
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
int ProtectedJob::cur_tick_idx = 0;
|
|
|
|
|
2012-01-09 10:25:55 -07:00
|
|
|
typedef std::map<std::pair<int,int>, bool> TMaterialCache;
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
static int max_history_days = 14;
|
|
|
|
|
2012-01-09 08:20:28 -07:00
|
|
|
struct ItemConstraint {
|
|
|
|
PersistentDataItem config;
|
|
|
|
|
2012-03-14 02:09:02 -06:00
|
|
|
bool is_craft;
|
2012-01-09 08:20:28 -07:00
|
|
|
ItemTypeInfo item;
|
|
|
|
|
|
|
|
MaterialInfo material;
|
2012-01-10 06:23:37 -07:00
|
|
|
df::dfhack_material_category mat_mask;
|
2012-01-09 08:20:28 -07:00
|
|
|
|
|
|
|
int weight;
|
|
|
|
std::vector<ProtectedJob*> jobs;
|
|
|
|
|
2012-08-12 23:39:11 -06:00
|
|
|
item_quality::item_quality min_quality;
|
2012-08-06 08:55:55 -06:00
|
|
|
|
2012-01-09 10:25:55 -07:00
|
|
|
int item_amount, item_count, item_inuse;
|
|
|
|
bool request_suspend, request_resume;
|
|
|
|
|
2012-01-15 04:39:20 -07:00
|
|
|
bool is_active, cant_resume_reported;
|
|
|
|
|
2012-01-09 10:25:55 -07:00
|
|
|
TMaterialCache material_cache;
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
deque<int> history;
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
public:
|
2012-01-15 04:39:20 -07:00
|
|
|
ItemConstraint()
|
2012-08-12 23:42:02 -06:00
|
|
|
: is_craft(false), weight(0), min_quality(item_quality::Ordinary),item_amount(0),
|
|
|
|
item_count(0), item_inuse(0), is_active(false), cant_resume_reported(false)
|
2012-01-15 04:39:20 -07:00
|
|
|
{}
|
2012-01-09 08:20:28 -07:00
|
|
|
|
|
|
|
int goalCount() { return config.ival(0); }
|
|
|
|
void setGoalCount(int v) { config.ival(0) = v; }
|
|
|
|
|
|
|
|
int goalGap() {
|
|
|
|
int gcnt = std::max(1, goalCount()/2);
|
|
|
|
return std::min(gcnt, config.ival(1) <= 0 ? 5 : config.ival(1));
|
|
|
|
}
|
|
|
|
void setGoalGap(int v) { config.ival(1) = v; }
|
2012-01-09 10:25:55 -07:00
|
|
|
|
|
|
|
bool goalByCount() { return config.ival(2) & 1; }
|
|
|
|
void setGoalByCount(bool v) {
|
|
|
|
if (v)
|
|
|
|
config.ival(2) |= 1;
|
|
|
|
else
|
|
|
|
config.ival(2) &= ~1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void init(const std::string &str)
|
|
|
|
{
|
|
|
|
config.val() = str;
|
|
|
|
config.ival(2) = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void computeRequest()
|
|
|
|
{
|
|
|
|
int size = goalByCount() ? item_count : item_amount;
|
|
|
|
request_resume = (size <= goalCount()-goalGap());
|
|
|
|
request_suspend = (size >= goalCount());
|
2012-10-24 05:09:34 -06:00
|
|
|
|
|
|
|
if (max_history_days > 0)
|
|
|
|
{
|
|
|
|
history.push_back(size);
|
|
|
|
if (history.size() > max_history_days * 2)
|
|
|
|
history.pop_front();
|
|
|
|
}
|
|
|
|
|
2012-01-09 10:25:55 -07:00
|
|
|
}
|
2012-01-09 08:20:28 -07:00
|
|
|
};
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
/******************************
|
|
|
|
* GLOBAL VARIABLES *
|
|
|
|
******************************/
|
2012-01-09 06:29:28 -07:00
|
|
|
|
|
|
|
static bool enabled = false;
|
|
|
|
static PersistentDataItem config;
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
static int last_tick_frame_count = 0;
|
|
|
|
static int last_frame_count = 0;
|
|
|
|
|
2012-01-09 06:29:28 -07:00
|
|
|
enum ConfigFlags {
|
2012-01-12 09:07:53 -07:00
|
|
|
CF_ENABLED = 1,
|
|
|
|
CF_DRYBUCKETS = 2,
|
|
|
|
CF_AUTOMELT = 4
|
2012-01-09 06:29:28 -07:00
|
|
|
};
|
2012-01-07 10:47:23 -07:00
|
|
|
|
|
|
|
typedef std::map<int, ProtectedJob*> TKnownJobs;
|
|
|
|
static TKnownJobs known_jobs;
|
|
|
|
|
|
|
|
static std::vector<ProtectedJob*> pending_recover;
|
2012-01-09 08:20:28 -07:00
|
|
|
static std::vector<ItemConstraint*> constraints;
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-01-12 09:07:53 -07:00
|
|
|
static int meltable_count = 0;
|
2012-01-15 04:39:20 -07:00
|
|
|
static bool melt_active = false;
|
2012-01-12 09:07:53 -07:00
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
/******************************
|
|
|
|
* MISC FUNCTIONS *
|
|
|
|
******************************/
|
2012-01-09 06:29:28 -07:00
|
|
|
|
2012-01-07 10:47:23 -07:00
|
|
|
static ProtectedJob *get_known(int id)
|
|
|
|
{
|
|
|
|
TKnownJobs::iterator it = known_jobs.find(id);
|
|
|
|
return (it != known_jobs.end()) ? it->second : NULL;
|
|
|
|
}
|
|
|
|
|
2012-01-09 06:29:28 -07:00
|
|
|
static bool isSupportedJob(df::job *job)
|
|
|
|
{
|
2012-05-03 01:47:04 -06:00
|
|
|
return job->specific_refs.empty() &&
|
2012-04-10 01:43:36 -06:00
|
|
|
Job::getHolder(job) &&
|
2012-01-10 06:23:37 -07:00
|
|
|
(!job->job_items.empty() ||
|
|
|
|
job->job_type == job_type::CollectClay ||
|
|
|
|
job->job_type == job_type::CollectSand);
|
2012-01-09 06:29:28 -07:00
|
|
|
}
|
|
|
|
|
2012-01-07 10:47:23 -07:00
|
|
|
static void enumLiveJobs(std::map<int, df::job*> &rv)
|
|
|
|
{
|
|
|
|
df::job_list_link *p = world->job_list.next;
|
|
|
|
for (; p; p = p->next)
|
|
|
|
rv[p->item->id] = p->item;
|
|
|
|
}
|
|
|
|
|
2012-01-12 09:07:53 -07:00
|
|
|
static bool isOptionEnabled(unsigned flag)
|
|
|
|
{
|
|
|
|
return config.isValid() && (config.ival(0) & flag) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setOptionEnabled(ConfigFlags flag, bool on)
|
|
|
|
{
|
|
|
|
if (!config.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (on)
|
|
|
|
config.ival(0) |= flag;
|
|
|
|
else
|
|
|
|
config.ival(0) &= ~flag;
|
|
|
|
}
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
/******************************
|
|
|
|
* STATE INITIALIZATION *
|
|
|
|
******************************/
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void stop_protect(color_ostream &out)
|
2012-01-09 06:29:28 -07:00
|
|
|
{
|
2012-01-07 10:47:23 -07:00
|
|
|
pending_recover.clear();
|
|
|
|
|
2012-01-09 06:29:28 -07:00
|
|
|
if (!known_jobs.empty())
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("Unprotecting %d jobs.\n", known_jobs.size());
|
2012-01-09 06:29:28 -07:00
|
|
|
|
2012-01-07 10:47:23 -07:00
|
|
|
for (TKnownJobs::iterator it = known_jobs.begin(); it != known_jobs.end(); ++it)
|
|
|
|
delete it->second;
|
|
|
|
|
|
|
|
known_jobs.clear();
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void cleanup_state(color_ostream &out)
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
2012-01-09 06:29:28 -07:00
|
|
|
config = PersistentDataItem();
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
stop_protect(out);
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < constraints.size(); i++)
|
2012-01-09 08:20:28 -07:00
|
|
|
delete constraints[i];
|
|
|
|
constraints.clear();
|
2012-01-09 06:29:28 -07:00
|
|
|
}
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void check_lost_jobs(color_ostream &out, int ticks);
|
|
|
|
static ItemConstraint *get_constraint(color_ostream &out, const std::string &str, PersistentDataItem *cfg = NULL);
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void start_protect(color_ostream &out)
|
2012-01-09 06:29:28 -07:00
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
check_lost_jobs(out, 0);
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-01-09 06:29:28 -07:00
|
|
|
if (!known_jobs.empty())
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("Protecting %d jobs.\n", known_jobs.size());
|
2012-01-09 06:29:28 -07:00
|
|
|
}
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-11-10 22:18:54 -07:00
|
|
|
static bool first_update_done = false;
|
2012-03-10 04:55:42 -07:00
|
|
|
static void init_state(color_ostream &out)
|
2012-01-09 06:29:28 -07:00
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
auto pworld = Core::getInstance().getWorld();
|
|
|
|
|
|
|
|
config = pworld->GetPersistentData("workflow/config");
|
2012-01-12 09:07:53 -07:00
|
|
|
if (config.isValid() && config.ival(0) == -1)
|
|
|
|
config.ival(0) = 0;
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-01-12 09:07:53 -07:00
|
|
|
enabled = isOptionEnabled(CF_ENABLED);
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-01-09 08:20:28 -07:00
|
|
|
// Parse constraints
|
|
|
|
std::vector<PersistentDataItem> items;
|
2012-03-10 04:55:42 -07:00
|
|
|
pworld->GetPersistentData(&items, "workflow/constraints");
|
2012-01-09 08:20:28 -07:00
|
|
|
|
|
|
|
for (int i = items.size()-1; i >= 0; i--) {
|
2012-03-10 04:55:42 -07:00
|
|
|
if (get_constraint(out, items[i].val(), &items[i]))
|
2012-01-09 08:20:28 -07:00
|
|
|
continue;
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr("Lost constraint %s\n", items[i].val().c_str());
|
|
|
|
pworld->DeletePersistentData(items[i]);
|
2012-01-09 08:20:28 -07:00
|
|
|
}
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
last_tick_frame_count = world->frame_counter;
|
|
|
|
last_frame_count = world->frame_counter;
|
2012-11-10 22:18:54 -07:00
|
|
|
first_update_done = false;
|
2012-01-11 09:04:04 -07:00
|
|
|
|
2012-01-09 06:29:28 -07:00
|
|
|
if (!enabled)
|
|
|
|
return;
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
start_protect(out);
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void enable_plugin(color_ostream &out)
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
auto pworld = Core::getInstance().getWorld();
|
|
|
|
|
2012-01-09 06:29:28 -07:00
|
|
|
if (!config.isValid())
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
config = pworld->AddPersistentData("workflow/config");
|
2012-01-09 06:29:28 -07:00
|
|
|
config.ival(0) = 0;
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
|
|
|
|
2012-01-12 09:07:53 -07:00
|
|
|
setOptionEnabled(CF_ENABLED, true);
|
2012-01-09 06:29:28 -07:00
|
|
|
enabled = true;
|
2012-11-05 11:48:24 -07:00
|
|
|
out << "Enabling workflow plugin." << endl;
|
|
|
|
|
|
|
|
for (vector<ItemConstraint *>::iterator it = constraints.begin(); it != constraints.end(); it++)
|
|
|
|
{
|
|
|
|
(*it)->history.clear();
|
|
|
|
}
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
start_protect(out);
|
2012-11-05 11:48:24 -07:00
|
|
|
first_update_done = false;
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
/******************************
|
|
|
|
* JOB AUTO-RECOVERY *
|
|
|
|
******************************/
|
2012-01-09 06:29:28 -07:00
|
|
|
|
2012-11-10 23:42:30 -07:00
|
|
|
static void forget_job(const color_ostream &out, ProtectedJob *pj)
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
|
|
|
known_jobs.erase(pj->id);
|
|
|
|
delete pj;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static bool recover_job(color_ostream &out, ProtectedJob *pj)
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
2012-01-11 09:04:04 -07:00
|
|
|
if (pj->isLive())
|
|
|
|
return true;
|
|
|
|
|
2012-01-07 10:47:23 -07:00
|
|
|
// Check that the building exists
|
|
|
|
pj->holder = df::building::find(pj->building_id);
|
|
|
|
if (!pj->holder)
|
|
|
|
{
|
2012-03-31 12:26:41 -06:00
|
|
|
out.printerr("Forgetting job %d (%s): holder building lost.\n",
|
2012-03-17 02:52:22 -06:00
|
|
|
pj->id, ENUM_KEY_STR(job_type, pj->job_copy->job_type).c_str());
|
2012-03-10 04:55:42 -07:00
|
|
|
forget_job(out, pj);
|
2012-01-07 10:47:23 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check its state and postpone or cancel if invalid
|
|
|
|
if (pj->holder->jobs.size() >= 10)
|
|
|
|
{
|
2012-03-31 12:26:41 -06:00
|
|
|
out.printerr("Forgetting job %d (%s): holder building has too many jobs.\n",
|
2012-03-17 02:52:22 -06:00
|
|
|
pj->id, ENUM_KEY_STR(job_type, pj->job_copy->job_type).c_str());
|
2012-03-10 04:55:42 -07:00
|
|
|
forget_job(out, pj);
|
2012-01-07 10:47:23 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pj->holder->jobs.empty())
|
|
|
|
{
|
|
|
|
df::job_type ftype = pj->holder->jobs[0]->job_type;
|
|
|
|
if (ftype == job_type::DestroyBuilding)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ENUM_ATTR(job_type,type,ftype) == job_type_class::StrangeMood)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-09 06:29:28 -07:00
|
|
|
// Create and link in the actual job structure
|
2012-04-10 01:43:36 -06:00
|
|
|
df::job *recovered = Job::cloneJobStruct(pj->job_copy);
|
2012-01-09 06:29:28 -07:00
|
|
|
|
2012-04-10 01:43:36 -06:00
|
|
|
if (!Job::linkIntoWorld(recovered, false)) // reuse same id
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
2012-04-10 01:43:36 -06:00
|
|
|
Job::deleteJobStruct(recovered);
|
2012-01-09 06:29:28 -07:00
|
|
|
|
2012-03-31 12:26:41 -06:00
|
|
|
out.printerr("Inconsistency: job %d (%s) already in list.\n",
|
2012-03-17 02:52:22 -06:00
|
|
|
pj->id, ENUM_KEY_STR(job_type, pj->job_copy->job_type).c_str());
|
2012-01-07 10:47:23 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
pj->holder->jobs.push_back(recovered);
|
|
|
|
|
|
|
|
// Done
|
2012-01-11 09:04:04 -07:00
|
|
|
pj->recover(recovered);
|
2012-01-07 10:47:23 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
static ProtectedJob *add_known_job(df::job *job)
|
|
|
|
{
|
|
|
|
ProtectedJob *pj = new ProtectedJob(job);
|
|
|
|
assert(pj->holder);
|
|
|
|
known_jobs[pj->id] = pj;
|
|
|
|
|
|
|
|
return pj;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void check_lost_jobs(color_ostream &out, int ticks)
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
2012-01-11 09:04:04 -07:00
|
|
|
ProtectedJob::cur_tick_idx++;
|
|
|
|
if (ticks < 0) ticks = 0;
|
2012-01-07 10:47:23 -07:00
|
|
|
|
|
|
|
df::job_list_link *p = world->job_list.next;
|
|
|
|
for (; p; p = p->next)
|
|
|
|
{
|
2012-01-09 06:29:28 -07:00
|
|
|
df::job *job = p->item;
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-01-09 06:29:28 -07:00
|
|
|
ProtectedJob *pj = get_known(job->id);
|
|
|
|
if (pj)
|
|
|
|
{
|
|
|
|
if (!job->flags.bits.repeat)
|
2012-03-10 04:55:42 -07:00
|
|
|
forget_job(out, pj);
|
2012-01-09 06:29:28 -07:00
|
|
|
else
|
2012-01-11 09:04:04 -07:00
|
|
|
pj->tick_job(job, ticks);
|
2012-01-09 06:29:28 -07:00
|
|
|
}
|
|
|
|
else if (job->flags.bits.repeat && isSupportedJob(job))
|
|
|
|
{
|
2012-10-24 05:09:34 -06:00
|
|
|
add_known_job(job);
|
2012-01-09 06:29:28 -07:00
|
|
|
}
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for (TKnownJobs::const_iterator it = known_jobs.begin(); it != known_jobs.end(); ++it)
|
|
|
|
{
|
2012-01-11 09:04:04 -07:00
|
|
|
if (it->second->tick_idx == ProtectedJob::cur_tick_idx ||
|
|
|
|
!it->second->isLive())
|
2012-01-07 10:47:23 -07:00
|
|
|
continue;
|
|
|
|
|
2012-01-09 08:20:28 -07:00
|
|
|
it->second->actual_job = NULL;
|
2012-01-07 10:47:23 -07:00
|
|
|
pending_recover.push_back(it->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void update_job_data(color_ostream &out)
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
|
|
|
df::job_list_link *p = world->job_list.next;
|
|
|
|
for (; p; p = p->next)
|
|
|
|
{
|
|
|
|
ProtectedJob *pj = get_known(p->item->id);
|
|
|
|
if (!pj)
|
|
|
|
continue;
|
|
|
|
pj->update(p->item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void recover_jobs(color_ostream &out)
|
2012-01-09 06:29:28 -07:00
|
|
|
{
|
|
|
|
for (int i = pending_recover.size()-1; i >= 0; i--)
|
2012-03-10 04:55:42 -07:00
|
|
|
if (recover_job(out, pending_recover[i]))
|
2012-01-09 06:29:28 -07:00
|
|
|
vector_erase_at(pending_recover, i);
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void process_constraints(color_ostream &out);
|
2012-01-09 10:25:55 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_onupdate(color_ostream &out)
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
2012-01-09 06:29:28 -07:00
|
|
|
if (!enabled)
|
2012-01-07 10:47:23 -07:00
|
|
|
return CR_OK;
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
// Every 5 frames check the jobs for disappearance
|
2012-01-07 10:47:23 -07:00
|
|
|
static unsigned cnt = 0;
|
2012-11-05 11:48:24 -07:00
|
|
|
if ((++cnt % 5) != 0 && first_update_done)
|
2012-01-11 09:04:04 -07:00
|
|
|
return CR_OK;
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
check_lost_jobs(out, world->frame_counter - last_tick_frame_count);
|
2012-01-11 09:04:04 -07:00
|
|
|
last_tick_frame_count = world->frame_counter;
|
|
|
|
|
|
|
|
// Proceed every in-game half-day, or when jobs to recover changed
|
2012-01-09 08:20:28 -07:00
|
|
|
static unsigned last_rlen = 0;
|
2012-01-11 09:04:04 -07:00
|
|
|
bool check_time = (world->frame_counter - last_frame_count) >= DAY_TICKS/2;
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
if (pending_recover.size() != last_rlen || check_time || !first_update_done)
|
2012-01-09 10:25:55 -07:00
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
recover_jobs(out);
|
2012-01-11 09:04:04 -07:00
|
|
|
last_rlen = pending_recover.size();
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
// If the half-day passed, proceed to update
|
2012-10-24 05:09:34 -06:00
|
|
|
if (check_time || !first_update_done)
|
2012-01-09 08:20:28 -07:00
|
|
|
{
|
2012-01-11 09:04:04 -07:00
|
|
|
last_frame_count = world->frame_counter;
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
update_job_data(out);
|
|
|
|
process_constraints(out);
|
2012-10-24 05:09:34 -06:00
|
|
|
first_update_done = true;
|
2012-01-09 08:20:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
|
2012-01-09 08:20:28 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
/******************************
|
|
|
|
* ITEM COUNT CONSTRAINT *
|
|
|
|
******************************/
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
static string get_constraint_material(ItemConstraint *cv)
|
|
|
|
{
|
|
|
|
string text;
|
|
|
|
if (!cv->material.isNone())
|
|
|
|
{
|
|
|
|
text.append(cv->material.toString());
|
|
|
|
text.append(" ");
|
|
|
|
}
|
|
|
|
|
2012-11-09 07:47:15 -07:00
|
|
|
text.append(bitfield_to_string(cv->mat_mask, ","));
|
2012-11-05 11:48:24 -07:00
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2012-11-10 07:00:55 -07:00
|
|
|
#define ITEMDEF_VECTORS \
|
|
|
|
ITEM(WEAPON, weapons, itemdef_weaponst) \
|
|
|
|
ITEM(TRAPCOMP, trapcomps, itemdef_trapcompst) \
|
|
|
|
ITEM(TOY, toys, itemdef_toyst) \
|
|
|
|
ITEM(TOOL, tools, itemdef_toolst) \
|
|
|
|
ITEM(INSTRUMENT, instruments, itemdef_instrumentst) \
|
|
|
|
ITEM(ARMOR, armor, itemdef_armorst) \
|
|
|
|
ITEM(AMMO, ammo, itemdef_ammost) \
|
|
|
|
ITEM(SIEGEAMMO, siege_ammo, itemdef_siegeammost) \
|
|
|
|
ITEM(GLOVES, gloves, itemdef_glovesst) \
|
|
|
|
ITEM(SHOES, shoes, itemdef_shoesst) \
|
|
|
|
ITEM(SHIELD, shields, itemdef_shieldst) \
|
|
|
|
ITEM(HELM, helms, itemdef_helmst) \
|
|
|
|
ITEM(PANTS, pants, itemdef_pantsst) \
|
|
|
|
ITEM(FOOD, food, itemdef_foodst)
|
|
|
|
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
static ItemConstraint * create_new_constraint(bool is_craft, ItemTypeInfo item, MaterialInfo material,
|
|
|
|
df::dfhack_material_category mat_mask, item_quality::item_quality minqual,
|
2012-11-05 11:48:24 -07:00
|
|
|
PersistentDataItem * cfg, std::string str)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
for (size_t i = 0; i < constraints.size(); i++)
|
|
|
|
{
|
|
|
|
ItemConstraint *ct = constraints[i];
|
|
|
|
if (ct->is_craft == is_craft &&
|
|
|
|
ct->item == item && ct->material == material &&
|
|
|
|
ct->mat_mask.whole == mat_mask.whole &&
|
|
|
|
ct->min_quality == minqual)
|
|
|
|
return ct;
|
|
|
|
}
|
|
|
|
|
|
|
|
int weight = 0;
|
|
|
|
if (item.subtype >= 0)
|
|
|
|
weight += 10000;
|
|
|
|
if (mat_mask.whole != 0)
|
|
|
|
weight += 100;
|
|
|
|
if (material.type >= 0)
|
|
|
|
weight += (material.index >= 0 ? 5000 : 1000);
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
ItemConstraint *nct = new ItemConstraint;
|
|
|
|
nct->is_craft = is_craft;
|
|
|
|
nct->item = item;
|
|
|
|
nct->material = material;
|
|
|
|
nct->mat_mask = mat_mask;
|
|
|
|
nct->min_quality = minqual;
|
|
|
|
nct->weight = weight;
|
|
|
|
|
|
|
|
if (cfg)
|
|
|
|
nct->config = *cfg;
|
|
|
|
else
|
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
if (str.empty())
|
|
|
|
{
|
2012-11-10 07:00:55 -07:00
|
|
|
std::string item_token = ENUM_KEY_STR(item_type, item.type);
|
|
|
|
if (item.custom)
|
|
|
|
item_token += ":" + item.custom->id;
|
|
|
|
else if (item.subtype != -1)
|
|
|
|
{
|
|
|
|
df::world_raws::T_itemdefs &defs = df::global::world->raws.itemdefs;
|
|
|
|
switch (item.type)
|
|
|
|
{
|
|
|
|
#define ITEM(type_string,vec,tclass) \
|
|
|
|
case df::item_type::type_string: \
|
|
|
|
item_token += ":" + defs.vec[item.subtype]->id; \
|
|
|
|
break;
|
|
|
|
|
|
|
|
ITEMDEF_VECTORS
|
|
|
|
#undef ITEM
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
str.append(item_token);
|
|
|
|
str.append("/");
|
|
|
|
str.append(bitfield_to_string(mat_mask, ","));
|
|
|
|
str.append("/");
|
|
|
|
if ((material.type != 0 || material.index > 0) && !material.isNone())
|
|
|
|
str.append(material.getToken());
|
2012-11-05 11:48:24 -07:00
|
|
|
}
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
nct->config = Core::getInstance().getWorld()->AddPersistentData("workflow/constraints");
|
|
|
|
nct->init(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
constraints.push_back(nct);
|
|
|
|
return nct;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static ItemConstraint *get_constraint(color_ostream &out, const std::string &str, PersistentDataItem *cfg)
|
2012-01-09 08:20:28 -07:00
|
|
|
{
|
|
|
|
std::vector<std::string> tokens;
|
|
|
|
split_string(&tokens, str, "/");
|
|
|
|
|
2012-08-06 08:55:55 -06:00
|
|
|
if (tokens.size() > 4)
|
2012-01-09 08:20:28 -07:00
|
|
|
return NULL;
|
|
|
|
|
2012-03-14 02:09:02 -06:00
|
|
|
bool is_craft = false;
|
2012-01-09 08:20:28 -07:00
|
|
|
ItemTypeInfo item;
|
2012-03-14 02:09:02 -06:00
|
|
|
|
|
|
|
if (tokens[0] == "ANY_CRAFT" || tokens[0] == "CRAFTS") {
|
|
|
|
is_craft = true;
|
|
|
|
} else if (!item.find(tokens[0]) || !item.isValid()) {
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr("Cannot find item type: %s\n", tokens[0].c_str());
|
2012-01-09 08:20:28 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
df::dfhack_material_category mat_mask;
|
|
|
|
std::string maskstr = vector_get(tokens,1);
|
|
|
|
if (!maskstr.empty() && !parseJobMaterialCategory(&mat_mask, maskstr)) {
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr("Cannot decode material mask: %s\n", maskstr.c_str());
|
2012-01-10 06:23:37 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-08-06 08:55:55 -06:00
|
|
|
|
2012-01-09 08:20:28 -07:00
|
|
|
MaterialInfo material;
|
2012-01-10 06:23:37 -07:00
|
|
|
std::string matstr = vector_get(tokens,2);
|
2012-01-09 08:20:28 -07:00
|
|
|
if (!matstr.empty() && (!material.find(matstr) || !material.isValid())) {
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr("Cannot find material: %s\n", matstr.c_str());
|
2012-01-09 08:20:28 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-12 23:42:02 -06:00
|
|
|
item_quality::item_quality minqual = item_quality::Ordinary;
|
2012-08-06 08:55:55 -06:00
|
|
|
std::string qualstr = vector_get(tokens, 3);
|
|
|
|
if(!qualstr.empty()) {
|
2012-08-12 23:42:02 -06:00
|
|
|
if(qualstr == "ordinary") minqual = item_quality::Ordinary;
|
|
|
|
else if(qualstr == "wellcrafted") minqual = item_quality::WellCrafted;
|
|
|
|
else if(qualstr == "finelycrafted") minqual = item_quality::FinelyCrafted;
|
|
|
|
else if(qualstr == "superior") minqual = item_quality::Superior;
|
|
|
|
else if(qualstr == "exceptional") minqual = item_quality::Exceptional;
|
|
|
|
else if(qualstr == "masterful") minqual = item_quality::Masterful;
|
2012-08-06 08:55:55 -06:00
|
|
|
else {
|
|
|
|
out.printerr("Cannot find quality: %s\nKnown qualities: ordinary, wellcrafted, finelycrafted, superior, exceptional, masterful\n", qualstr.c_str());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-09 08:20:28 -07:00
|
|
|
if (mat_mask.whole && material.isValid() && !material.matches(mat_mask)) {
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr("Material %s doesn't match mask %s\n", matstr.c_str(), maskstr.c_str());
|
2012-01-09 08:20:28 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
ItemConstraint *nct = create_new_constraint(is_craft, item, material, mat_mask, minqual, cfg, str);
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-01-09 08:20:28 -07:00
|
|
|
return nct;
|
|
|
|
}
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-03-13 18:52:58 -06:00
|
|
|
static void delete_constraint(ItemConstraint *cv)
|
2012-01-09 08:20:28 -07:00
|
|
|
{
|
|
|
|
int idx = linear_index(constraints, cv);
|
|
|
|
if (idx >= 0)
|
2012-11-05 11:48:24 -07:00
|
|
|
{
|
|
|
|
for (vector<ProtectedJob *>::iterator it = constraints[idx]->jobs.begin(); it != constraints[idx]->jobs.end(); it++)
|
|
|
|
{
|
|
|
|
int idpj = linear_index((*it)->constraints, cv);
|
|
|
|
if (idpj >= 0)
|
|
|
|
vector_erase_at((*it)->constraints, idpj);
|
|
|
|
}
|
2012-01-09 08:20:28 -07:00
|
|
|
vector_erase_at(constraints, idx);
|
2012-11-05 11:48:24 -07:00
|
|
|
}
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
Core::getInstance().getWorld()->DeletePersistentData(cv->config);
|
2012-01-09 08:20:28 -07:00
|
|
|
delete cv;
|
|
|
|
}
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
/******************************
|
|
|
|
* JOB-CONSTRAINT MAPPING *
|
|
|
|
******************************/
|
|
|
|
|
2012-03-14 02:09:02 -06:00
|
|
|
static bool isCraftItem(df::item_type type)
|
|
|
|
{
|
|
|
|
using namespace df::enums::job_type;
|
|
|
|
|
|
|
|
auto lst = ENUM_ATTR(job_type, possible_item, MakeCrafts);
|
|
|
|
for (size_t i = 0; i < lst.size; i++)
|
|
|
|
if (lst.items[i] == type)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
static void link_job_constraint(ProtectedJob *pj, df::item_type itype, int16_t isubtype,
|
|
|
|
df::dfhack_material_category mat_mask,
|
2012-03-14 02:09:02 -06:00
|
|
|
int16_t mat_type, int32_t mat_index,
|
2012-10-24 05:09:34 -06:00
|
|
|
bool is_craft = false, bool create_constraint = false)
|
2012-01-09 08:20:28 -07:00
|
|
|
{
|
2012-01-10 06:23:37 -07:00
|
|
|
MaterialInfo mat(mat_type, mat_index);
|
2012-01-09 10:25:55 -07:00
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
bool constraint_found = false;
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < constraints.size(); i++)
|
2012-01-10 06:23:37 -07:00
|
|
|
{
|
|
|
|
ItemConstraint *ct = constraints[i];
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-03-14 02:09:02 -06:00
|
|
|
if (is_craft)
|
|
|
|
{
|
|
|
|
if (!ct->is_craft && !isCraftItem(ct->item.type))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ct->item.type != itype ||
|
|
|
|
(ct->item.subtype != -1 && ct->item.subtype != isubtype))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
if (!mat.matches(ct->material))
|
|
|
|
continue;
|
|
|
|
if (ct->mat_mask.whole)
|
|
|
|
{
|
|
|
|
if (mat.isValid())
|
|
|
|
{
|
|
|
|
if (!mat.matches(ct->mat_mask))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!(mat_mask.whole & ct->mat_mask.whole))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
if (linear_index(pj->constraints, ct) >= 0)
|
|
|
|
continue;
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
ct->jobs.push_back(pj);
|
|
|
|
pj->constraints.push_back(ct);
|
2012-10-24 05:09:34 -06:00
|
|
|
constraint_found = true;
|
2012-01-15 04:39:20 -07:00
|
|
|
|
|
|
|
if (!ct->is_active && pj->isResumed())
|
|
|
|
ct->is_active = true;
|
2012-01-09 08:20:28 -07:00
|
|
|
}
|
2012-10-24 05:09:34 -06:00
|
|
|
|
|
|
|
if (create_constraint && !constraint_found)
|
|
|
|
{
|
|
|
|
ItemTypeInfo item;
|
|
|
|
item.type = itype;
|
|
|
|
item.subtype = isubtype;
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
ItemConstraint *nct = create_new_constraint(is_craft, item, mat, mat_mask, item_quality::Ordinary, NULL, "");
|
2012-10-24 05:09:34 -06:00
|
|
|
nct->jobs.push_back(pj);
|
|
|
|
pj->constraints.push_back(nct);
|
|
|
|
}
|
2012-01-09 08:20:28 -07:00
|
|
|
}
|
|
|
|
|
2012-11-10 07:00:55 -07:00
|
|
|
static void compute_custom_job(ProtectedJob *pj, df::job *job, bool create_constraint = false)
|
2012-01-09 08:20:28 -07:00
|
|
|
{
|
2012-01-10 06:23:37 -07:00
|
|
|
if (pj->reaction_id < 0)
|
|
|
|
pj->reaction_id = linear_index(df::reaction::get_vector(),
|
|
|
|
&df::reaction::code, job->reaction_name);
|
|
|
|
|
|
|
|
df::reaction *r = df::reaction::find(pj->reaction_id);
|
|
|
|
if (!r)
|
2012-01-09 08:20:28 -07:00
|
|
|
return;
|
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < r->products.size(); i++)
|
2012-01-10 06:23:37 -07:00
|
|
|
{
|
|
|
|
using namespace df::enums::reaction_product_item_flags;
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
VIRTUAL_CAST_VAR(prod, df::reaction_product_itemst, r->products[i]);
|
2012-09-10 07:19:21 -06:00
|
|
|
if (!prod || (prod->item_type < (df::item_type)0 && !prod->flags.is_set(CRAFTS)))
|
2012-01-10 06:23:37 -07:00
|
|
|
continue;
|
|
|
|
|
|
|
|
MaterialInfo mat(prod);
|
2012-01-16 08:45:38 -07:00
|
|
|
df::dfhack_material_category mat_mask(0);
|
2012-01-10 06:23:37 -07:00
|
|
|
|
|
|
|
bool get_mat_prod = prod->flags.is_set(GET_MATERIAL_PRODUCT);
|
|
|
|
if (get_mat_prod || prod->flags.is_set(GET_MATERIAL_SAME))
|
|
|
|
{
|
|
|
|
int reagent_idx = linear_index(r->reagents, &df::reaction_reagent::code,
|
|
|
|
prod->get_material.reagent_code);
|
|
|
|
if (reagent_idx < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int item_idx = linear_index(job->job_items, &df::job_item::reagent_index, reagent_idx);
|
|
|
|
if (item_idx >= 0)
|
|
|
|
mat.decode(job->job_items[item_idx]);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VIRTUAL_CAST_VAR(src, df::reaction_reagent_itemst, r->reagents[reagent_idx]);
|
|
|
|
if (!src)
|
|
|
|
continue;
|
|
|
|
mat.decode(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_mat_prod)
|
|
|
|
{
|
2012-01-16 08:45:38 -07:00
|
|
|
std::string code = prod->get_material.product_code;
|
|
|
|
|
|
|
|
if (mat.isValid())
|
|
|
|
{
|
|
|
|
int idx = linear_index(mat.material->reaction_product.id, code);
|
|
|
|
if (idx < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
mat.decode(mat.material->reaction_product.material, idx);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (code == "SOAP_MAT")
|
|
|
|
mat_mask.bits.soap = true;
|
|
|
|
}
|
2012-01-10 06:23:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
link_job_constraint(pj, prod->item_type, prod->item_subtype,
|
2012-11-10 07:00:55 -07:00
|
|
|
mat_mask, mat.type, mat.index, prod->flags.is_set(CRAFTS), create_constraint);
|
2012-01-10 06:23:37 -07:00
|
|
|
}
|
2012-01-09 08:20:28 -07:00
|
|
|
}
|
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
static void guess_job_material(df::job *job, MaterialInfo &mat, df::dfhack_material_category &mat_mask)
|
2012-01-09 08:20:28 -07:00
|
|
|
{
|
2012-01-10 06:23:37 -07:00
|
|
|
using namespace df::enums::job_type;
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
if (job->job_type == PrepareMeal)
|
|
|
|
mat.decode(-1);
|
|
|
|
else
|
|
|
|
mat.decode(job);
|
|
|
|
|
|
|
|
mat_mask.whole = job->material_category.whole;
|
|
|
|
|
|
|
|
// Material from the job enum
|
|
|
|
const char *job_material = ENUM_ATTR(job_type, material, job->job_type);
|
|
|
|
if (job_material)
|
2012-01-09 08:20:28 -07:00
|
|
|
{
|
2012-01-10 06:23:37 -07:00
|
|
|
MaterialInfo info;
|
|
|
|
if (info.findBuiltin(job_material))
|
|
|
|
mat = info;
|
|
|
|
else
|
|
|
|
parseJobMaterialCategory(&mat_mask, job_material);
|
|
|
|
}
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
// Material from the job reagent
|
2012-01-15 01:58:36 -07:00
|
|
|
if (!mat.isValid() && !job->job_items.empty() &&
|
|
|
|
(job->job_items.size() == 1 ||
|
|
|
|
job->job_items[0]->item_type == item_type::PLANT))
|
2012-01-10 06:23:37 -07:00
|
|
|
{
|
|
|
|
mat.decode(job->job_items[0]);
|
|
|
|
|
|
|
|
switch (job->job_items[0]->item_type)
|
|
|
|
{
|
|
|
|
case item_type::WOOD:
|
|
|
|
mat_mask.bits.wood = mat_mask.bits.wood2 = true;
|
|
|
|
break;
|
2012-04-20 03:30:37 -06:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2012-01-10 06:23:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
static void compute_job_outputs(color_ostream &out, ProtectedJob *pj, bool create_constraint = false)
|
2012-01-10 06:23:37 -07:00
|
|
|
{
|
|
|
|
using namespace df::enums::job_type;
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
// Custom reactions handled in another function
|
|
|
|
df::job *job = pj->job_copy;
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
if (job->job_type == CustomReaction)
|
|
|
|
{
|
2012-11-10 07:00:55 -07:00
|
|
|
compute_custom_job(pj, job, create_constraint);
|
2012-01-10 06:23:37 -07:00
|
|
|
return;
|
|
|
|
}
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
// Item type & subtype
|
|
|
|
df::item_type itype = ENUM_ATTR(job_type, item, job->job_type);
|
|
|
|
int16_t isubtype = job->item_subtype;
|
2012-03-14 02:09:02 -06:00
|
|
|
|
|
|
|
if (itype == item_type::NONE && job->job_type != MakeCrafts)
|
2012-01-10 06:23:37 -07:00
|
|
|
return;
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
// Item material & material category
|
|
|
|
MaterialInfo mat;
|
|
|
|
df::dfhack_material_category mat_mask;
|
|
|
|
guess_job_material(job, mat, mat_mask);
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
// Job-specific code
|
|
|
|
switch (job->job_type)
|
|
|
|
{
|
|
|
|
case SmeltOre:
|
|
|
|
if (mat.inorganic)
|
|
|
|
{
|
|
|
|
std::vector<int16_t> &ores = mat.inorganic->metal_ore.mat_index;
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < ores.size(); i++)
|
2012-01-10 06:23:37 -07:00
|
|
|
link_job_constraint(pj, item_type::BAR, -1, 0, 0, ores[i]);
|
2012-01-09 08:20:28 -07:00
|
|
|
}
|
2012-01-10 06:23:37 -07:00
|
|
|
return;
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
case ExtractMetalStrands:
|
|
|
|
if (mat.inorganic)
|
|
|
|
{
|
|
|
|
std::vector<int16_t> &threads = mat.inorganic->thread_metal.mat_index;
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < threads.size(); i++)
|
2012-01-10 06:23:37 -07:00
|
|
|
link_job_constraint(pj, item_type::THREAD, -1, 0, 0, threads[i]);
|
|
|
|
}
|
|
|
|
return;
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
case PrepareMeal:
|
|
|
|
if (job->mat_type != -1)
|
2012-01-09 08:20:28 -07:00
|
|
|
{
|
2012-01-10 06:23:37 -07:00
|
|
|
std::vector<df::itemdef_foodst*> &food = df::itemdef_foodst::get_vector();
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < food.size(); i++)
|
2012-01-10 06:23:37 -07:00
|
|
|
if (food[i]->level == job->mat_type)
|
|
|
|
link_job_constraint(pj, item_type::FOOD, i, 0, -1, -1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-03-14 02:09:02 -06:00
|
|
|
case MakeCrafts:
|
|
|
|
link_job_constraint(pj, item_type::NONE, -1, mat_mask, mat.type, mat.index, true);
|
|
|
|
return;
|
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
#define PLANT_PROCESS_MAT(flag, tag) \
|
|
|
|
if (mat.plant && mat.plant->flags.is_set(plant_raw_flags::flag)) \
|
|
|
|
mat.decode(mat.plant->material_defs.type_##tag, \
|
|
|
|
mat.plant->material_defs.idx_##tag); \
|
|
|
|
else mat.decode(-1);
|
2012-05-22 07:50:48 -06:00
|
|
|
case BrewDrink:
|
|
|
|
PLANT_PROCESS_MAT(DRINK, drink);
|
|
|
|
break;
|
2012-01-10 06:23:37 -07:00
|
|
|
case MillPlants:
|
|
|
|
PLANT_PROCESS_MAT(MILL, mill);
|
|
|
|
break;
|
|
|
|
case ProcessPlants:
|
|
|
|
PLANT_PROCESS_MAT(THREAD, thread);
|
|
|
|
break;
|
|
|
|
case ProcessPlantsBag:
|
|
|
|
PLANT_PROCESS_MAT(LEAVES, leaves);
|
|
|
|
break;
|
|
|
|
case ProcessPlantsBarrel:
|
|
|
|
PLANT_PROCESS_MAT(EXTRACT_BARREL, extract_barrel);
|
|
|
|
break;
|
|
|
|
case ProcessPlantsVial:
|
|
|
|
PLANT_PROCESS_MAT(EXTRACT_VIAL, extract_vial);
|
|
|
|
break;
|
|
|
|
case ExtractFromPlants:
|
|
|
|
PLANT_PROCESS_MAT(EXTRACT_STILL_VIAL, extract_still_vial);
|
|
|
|
break;
|
|
|
|
#undef PLANT_PROCESS_MAT
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
link_job_constraint(pj, itype, isubtype, mat_mask, mat.type, mat.index, false, create_constraint);
|
2012-01-10 06:23:37 -07:00
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void map_job_constraints(color_ostream &out)
|
2012-01-10 06:23:37 -07:00
|
|
|
{
|
2012-01-15 04:39:20 -07:00
|
|
|
melt_active = false;
|
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < constraints.size(); i++)
|
2012-01-15 04:39:20 -07:00
|
|
|
{
|
2012-01-10 06:23:37 -07:00
|
|
|
constraints[i]->jobs.clear();
|
2012-01-15 04:39:20 -07:00
|
|
|
constraints[i]->is_active = false;
|
|
|
|
}
|
2012-01-10 06:23:37 -07:00
|
|
|
|
|
|
|
for (TKnownJobs::const_iterator it = known_jobs.begin(); it != known_jobs.end(); ++it)
|
|
|
|
{
|
2012-01-15 04:39:20 -07:00
|
|
|
ProtectedJob *pj = it->second;
|
|
|
|
|
|
|
|
pj->constraints.clear();
|
2012-01-10 06:23:37 -07:00
|
|
|
|
2012-01-15 04:39:20 -07:00
|
|
|
if (!pj->isLive())
|
2012-01-10 06:23:37 -07:00
|
|
|
continue;
|
|
|
|
|
2012-01-15 04:39:20 -07:00
|
|
|
if (!melt_active && pj->actual_job->job_type == job_type::MeltMetalObject)
|
|
|
|
melt_active = pj->isResumed();
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
compute_job_outputs(out, pj);
|
2012-01-09 08:20:28 -07:00
|
|
|
}
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
/******************************
|
|
|
|
* ITEM-CONSTRAINT MAPPING *
|
|
|
|
******************************/
|
|
|
|
|
2012-01-12 09:07:53 -07:00
|
|
|
static void dryBucket(df::item *item)
|
2012-01-09 10:25:55 -07:00
|
|
|
{
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < item->itemrefs.size(); i++)
|
2012-01-11 09:04:04 -07:00
|
|
|
{
|
|
|
|
df::general_ref *ref = item->itemrefs[i];
|
2012-01-31 09:17:27 -07:00
|
|
|
if (ref->getType() == general_ref_type::CONTAINS_ITEM)
|
2012-01-12 09:07:53 -07:00
|
|
|
{
|
|
|
|
df::item *obj = ref->getItem();
|
|
|
|
|
|
|
|
if (obj &&
|
|
|
|
obj->getType() == item_type::LIQUID_MISC &&
|
|
|
|
obj->getMaterial() == builtin_mats::WATER)
|
|
|
|
{
|
2012-03-30 00:44:52 -06:00
|
|
|
obj->flags.bits.garbage_collect = true;
|
2012-01-12 09:07:53 -07:00
|
|
|
obj->flags.bits.hidden = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool itemBusy(df::item *item)
|
|
|
|
{
|
2012-01-15 09:45:02 -07:00
|
|
|
using namespace df::enums::item_type;
|
2012-01-12 09:07:53 -07:00
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < item->itemrefs.size(); i++)
|
2012-01-12 09:07:53 -07:00
|
|
|
{
|
|
|
|
df::general_ref *ref = item->itemrefs[i];
|
2012-01-31 09:17:27 -07:00
|
|
|
if (ref->getType() == general_ref_type::CONTAINS_ITEM)
|
2012-01-12 09:07:53 -07:00
|
|
|
{
|
|
|
|
df::item *obj = ref->getItem();
|
2012-03-30 00:44:52 -06:00
|
|
|
if (obj && !obj->flags.bits.garbage_collect)
|
2012-01-12 09:07:53 -07:00
|
|
|
return true;
|
|
|
|
}
|
2012-01-31 09:17:27 -07:00
|
|
|
else if (ref->getType() == general_ref_type::CONTAINS_UNIT)
|
2012-01-09 10:25:55 -07:00
|
|
|
return true;
|
2012-01-31 09:17:27 -07:00
|
|
|
else if (ref->getType() == general_ref_type::UNIT_HOLDER)
|
2012-01-12 09:07:53 -07:00
|
|
|
{
|
|
|
|
if (!item->flags.bits.in_job)
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-31 09:17:27 -07:00
|
|
|
else if (ref->getType() == general_ref_type::CONTAINED_IN_ITEM)
|
2012-01-15 09:45:02 -07:00
|
|
|
{
|
|
|
|
df::item *obj = ref->getItem();
|
|
|
|
if (!obj)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Stuff in flasks and backpacks is busy
|
|
|
|
df::item_type type = obj->getType();
|
|
|
|
if ((type == FLASK && item->getType() == DRINK) || type == BACKPACK)
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-11 09:04:04 -07:00
|
|
|
}
|
2012-01-09 10:25:55 -07:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
static bool itemInRealJob(df::item *item)
|
|
|
|
{
|
|
|
|
if (!item->flags.bits.in_job)
|
|
|
|
return false;
|
|
|
|
|
2012-05-03 01:47:04 -06:00
|
|
|
auto ref = Items::getSpecificRef(item, specific_ref_type::JOB);
|
|
|
|
if (!ref || !ref->job)
|
2012-01-10 06:23:37 -07:00
|
|
|
return true;
|
|
|
|
|
2012-05-03 01:47:04 -06:00
|
|
|
return ENUM_ATTR(job_type, type, ref->job->job_type)
|
2012-01-10 06:23:37 -07:00
|
|
|
!= job_type_class::Hauling;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void map_job_items(color_ostream &out)
|
2012-01-09 10:25:55 -07:00
|
|
|
{
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < constraints.size(); i++)
|
2012-01-09 10:25:55 -07:00
|
|
|
{
|
|
|
|
constraints[i]->item_amount = 0;
|
|
|
|
constraints[i]->item_count = 0;
|
|
|
|
constraints[i]->item_inuse = 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 09:07:53 -07:00
|
|
|
meltable_count = 0;
|
|
|
|
|
2012-01-09 10:25:55 -07:00
|
|
|
// Precompute a bitmask with the bad flags
|
|
|
|
df::item_flags bad_flags;
|
|
|
|
bad_flags.whole = 0;
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
#define F(left_margin) bad_flags.bits.left_margin = true;
|
2012-03-30 00:44:52 -06:00
|
|
|
F(dump); F(forbid); F(garbage_collect);
|
2012-01-09 10:25:55 -07:00
|
|
|
F(hostile); F(on_fire); F(rotten); F(trader);
|
2012-01-11 09:04:04 -07:00
|
|
|
F(in_building); F(construction); F(artifact1);
|
2012-01-09 10:25:55 -07:00
|
|
|
#undef F
|
|
|
|
|
2012-01-12 09:07:53 -07:00
|
|
|
bool dry_buckets = isOptionEnabled(CF_DRYBUCKETS);
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
std::vector<df::item*> &items = world->items.other[items_other_id::ANY_FREE];
|
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < items.size(); i++)
|
2012-01-09 10:25:55 -07:00
|
|
|
{
|
|
|
|
df::item *item = items[i];
|
|
|
|
|
|
|
|
if (item->flags.whole & bad_flags.whole)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
df::item_type itype = item->getType();
|
|
|
|
int16_t isubtype = item->getSubtype();
|
|
|
|
int16_t imattype = item->getActualMaterial();
|
|
|
|
int32_t imatindex = item->getActualMaterialIndex();
|
|
|
|
|
2012-01-27 10:17:50 -07:00
|
|
|
bool is_invalid = false;
|
|
|
|
|
2012-04-07 11:11:53 -06:00
|
|
|
// don't count worn items
|
|
|
|
if (item->getWear() >= 1)
|
|
|
|
is_invalid = true;
|
|
|
|
|
2012-01-12 09:07:53 -07:00
|
|
|
// Special handling
|
2012-01-27 10:17:50 -07:00
|
|
|
switch (itype) {
|
|
|
|
case item_type::BUCKET:
|
|
|
|
if (dry_buckets && !item->flags.bits.in_job)
|
|
|
|
dryBucket(item);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case item_type::THREAD:
|
|
|
|
if (item->getTotalDimension() < 15000)
|
|
|
|
is_invalid = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case item_type::CLOTH:
|
|
|
|
if (item->getTotalDimension() < 10000)
|
|
|
|
is_invalid = true;
|
|
|
|
break;
|
2012-04-20 03:30:37 -06:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2012-01-27 10:17:50 -07:00
|
|
|
}
|
2012-01-12 09:07:53 -07:00
|
|
|
|
|
|
|
if (item->flags.bits.melt && !item->flags.bits.owned && !itemBusy(item))
|
|
|
|
meltable_count++;
|
|
|
|
|
|
|
|
// Match to constraints
|
2012-01-09 10:25:55 -07:00
|
|
|
TMaterialCache::key_type matkey(imattype, imatindex);
|
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < constraints.size(); i++)
|
2012-01-09 10:25:55 -07:00
|
|
|
{
|
|
|
|
ItemConstraint *cv = constraints[i];
|
2012-03-14 02:09:02 -06:00
|
|
|
|
|
|
|
if (cv->is_craft)
|
|
|
|
{
|
|
|
|
if (!isCraftItem(itype))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (cv->item.type != itype ||
|
|
|
|
(cv->item.subtype != -1 && cv->item.subtype != isubtype))
|
|
|
|
continue;
|
|
|
|
}
|
2012-08-06 08:55:55 -06:00
|
|
|
if(item->getQuality() < cv->min_quality) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-01-09 10:25:55 -07:00
|
|
|
|
|
|
|
TMaterialCache::iterator it = cv->material_cache.find(matkey);
|
|
|
|
|
|
|
|
bool ok = true;
|
|
|
|
if (it != cv->material_cache.end())
|
|
|
|
ok = it->second;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MaterialInfo mat(imattype, imatindex);
|
2012-01-10 06:23:37 -07:00
|
|
|
ok = mat.matches(cv->material) &&
|
|
|
|
(cv->mat_mask.whole == 0 || mat.matches(cv->mat_mask));
|
2012-01-09 10:25:55 -07:00
|
|
|
cv->material_cache[matkey] = ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
continue;
|
|
|
|
|
2012-01-27 10:17:50 -07:00
|
|
|
if (is_invalid ||
|
|
|
|
item->flags.bits.owned ||
|
2012-01-16 08:45:38 -07:00
|
|
|
item->flags.bits.in_chest ||
|
2012-01-10 06:23:37 -07:00
|
|
|
item->isAssignedToStockpile() ||
|
2012-01-12 09:07:53 -07:00
|
|
|
itemInRealJob(item) ||
|
|
|
|
itemBusy(item))
|
2012-01-10 06:23:37 -07:00
|
|
|
{
|
2012-01-09 10:25:55 -07:00
|
|
|
cv->item_inuse++;
|
2012-01-10 06:23:37 -07:00
|
|
|
}
|
2012-01-09 10:25:55 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
cv->item_count++;
|
|
|
|
cv->item_amount += item->getStackSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < constraints.size(); i++)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2012-01-09 10:25:55 -07:00
|
|
|
constraints[i]->computeRequest();
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
2012-01-09 10:25:55 -07:00
|
|
|
}
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
/******************************
|
|
|
|
* ITEM COUNT CONSTRAINT *
|
|
|
|
******************************/
|
|
|
|
|
|
|
|
static std::string shortJobDescription(df::job *job);
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void setJobResumed(color_ostream &out, ProtectedJob *pj, bool goal)
|
2012-01-12 09:07:53 -07:00
|
|
|
{
|
|
|
|
bool current = pj->isResumed();
|
|
|
|
|
|
|
|
pj->set_resumed(goal);
|
|
|
|
|
|
|
|
if (goal != current)
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("%s %s%s\n",
|
2012-01-12 09:07:53 -07:00
|
|
|
(goal ? "Resuming" : "Suspending"),
|
|
|
|
shortJobDescription(pj->actual_job).c_str(),
|
|
|
|
(!goal || pj->isActuallyResumed() ? "" : " (delayed)"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void update_jobs_by_constraints(color_ostream &out)
|
2012-01-09 10:25:55 -07:00
|
|
|
{
|
|
|
|
for (TKnownJobs::const_iterator it = known_jobs.begin(); it != known_jobs.end(); ++it)
|
|
|
|
{
|
|
|
|
ProtectedJob *pj = it->second;
|
2012-01-12 09:07:53 -07:00
|
|
|
if (!pj->isLive())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (pj->actual_job->job_type == job_type::MeltMetalObject &&
|
|
|
|
isOptionEnabled(CF_AUTOMELT))
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
setJobResumed(out, pj, meltable_count > 0);
|
2012-01-12 09:07:53 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pj->constraints.empty())
|
2012-01-09 10:25:55 -07:00
|
|
|
continue;
|
|
|
|
|
|
|
|
int resume_weight = -1;
|
|
|
|
int suspend_weight = -1;
|
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < pj->constraints.size(); i++)
|
2012-01-09 10:25:55 -07:00
|
|
|
{
|
|
|
|
if (pj->constraints[i]->request_resume)
|
|
|
|
resume_weight = std::max(resume_weight, pj->constraints[i]->weight);
|
|
|
|
if (pj->constraints[i]->request_suspend)
|
|
|
|
suspend_weight = std::max(suspend_weight, pj->constraints[i]->weight);
|
|
|
|
}
|
|
|
|
|
2012-01-12 09:07:53 -07:00
|
|
|
bool goal = pj->isResumed();
|
2012-01-09 10:25:55 -07:00
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
if (resume_weight >= 0 && resume_weight >= suspend_weight)
|
|
|
|
goal = true;
|
2012-01-11 09:04:04 -07:00
|
|
|
else if (suspend_weight >= 0 && suspend_weight >= resume_weight)
|
|
|
|
goal = false;
|
2012-01-09 10:25:55 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
setJobResumed(out, pj, goal);
|
2012-01-09 10:25:55 -07:00
|
|
|
}
|
2012-01-15 04:39:20 -07:00
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < constraints.size(); i++)
|
2012-01-15 04:39:20 -07:00
|
|
|
{
|
|
|
|
ItemConstraint *ct = constraints[i];
|
|
|
|
|
|
|
|
bool is_running = false;
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t j = 0; j < ct->jobs.size(); j++)
|
2012-01-15 04:39:20 -07:00
|
|
|
if (!!(is_running = ct->jobs[j]->isResumed()))
|
|
|
|
break;
|
|
|
|
|
|
|
|
std::string info = ct->item.toString();
|
|
|
|
|
2012-03-14 02:09:02 -06:00
|
|
|
if (ct->is_craft)
|
|
|
|
info = "crafts";
|
|
|
|
|
2012-01-15 04:39:20 -07:00
|
|
|
if (ct->material.isValid())
|
|
|
|
info = ct->material.toString() + " " + info;
|
|
|
|
else if (ct->mat_mask.whole)
|
2012-03-17 02:52:22 -06:00
|
|
|
info = bitfield_to_string(ct->mat_mask) + " " + info;
|
2012-01-15 04:39:20 -07:00
|
|
|
|
|
|
|
if (is_running != ct->is_active)
|
|
|
|
{
|
|
|
|
if (is_running && ct->request_resume)
|
2012-03-03 06:38:24 -07:00
|
|
|
Gui::showAnnouncement("Resuming production: " + info, 2, false);
|
2012-01-15 04:39:20 -07:00
|
|
|
else if (!is_running && !ct->request_resume)
|
2012-03-03 06:38:24 -07:00
|
|
|
Gui::showAnnouncement("Stopping production: " + info, 3, false);
|
2012-01-15 04:39:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ct->request_resume && !is_running)
|
|
|
|
{
|
|
|
|
if (!ct->cant_resume_reported)
|
2012-03-03 06:38:24 -07:00
|
|
|
Gui::showAnnouncement("Cannot produce: " + info, 6, true);
|
2012-01-15 04:39:20 -07:00
|
|
|
ct->cant_resume_reported = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ct->cant_resume_reported = false;
|
|
|
|
}
|
|
|
|
}
|
2012-01-09 10:25:55 -07:00
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void process_constraints(color_ostream &out)
|
2012-01-09 10:25:55 -07:00
|
|
|
{
|
2012-01-12 09:07:53 -07:00
|
|
|
if (constraints.empty() &&
|
|
|
|
!isOptionEnabled(CF_DRYBUCKETS | CF_AUTOMELT))
|
2012-01-09 10:25:55 -07:00
|
|
|
return;
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
map_job_constraints(out);
|
|
|
|
map_job_items(out);
|
|
|
|
update_jobs_by_constraints(out);
|
2012-01-09 10:25:55 -07:00
|
|
|
}
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
/******************************
|
|
|
|
* PRINTING AND THE COMMAND *
|
|
|
|
******************************/
|
|
|
|
|
|
|
|
static std::string shortJobDescription(df::job *job)
|
|
|
|
{
|
|
|
|
std::string rv = stl_sprintf("job %d: ", job->id);
|
|
|
|
|
|
|
|
if (job->job_type != job_type::CustomReaction)
|
|
|
|
rv += ENUM_KEY_STR(job_type, job->job_type);
|
|
|
|
else
|
|
|
|
rv += job->reaction_name;
|
|
|
|
|
|
|
|
MaterialInfo mat;
|
|
|
|
df::dfhack_material_category mat_mask;
|
|
|
|
guess_job_material(job, mat, mat_mask);
|
|
|
|
|
|
|
|
if (mat.isValid())
|
|
|
|
rv += " [" + mat.toString() + "]";
|
|
|
|
else if (mat_mask.whole)
|
2012-03-17 02:52:22 -06:00
|
|
|
rv += " [" + bitfield_to_string(mat_mask) + "]";
|
2012-01-11 09:04:04 -07:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2012-01-09 10:25:55 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void print_constraint(color_ostream &out, ItemConstraint *cv, bool no_job = false, std::string prefix = "")
|
2012-01-10 06:23:37 -07:00
|
|
|
{
|
2012-01-11 09:04:04 -07:00
|
|
|
Console::color_value color;
|
|
|
|
if (cv->request_resume)
|
2012-08-18 23:21:25 -06:00
|
|
|
color = COLOR_GREEN;
|
2012-01-11 09:04:04 -07:00
|
|
|
else if (cv->request_suspend)
|
2012-08-18 23:21:25 -06:00
|
|
|
color = COLOR_CYAN;
|
2012-01-11 09:04:04 -07:00
|
|
|
else
|
2012-08-18 23:21:25 -06:00
|
|
|
color = COLOR_DARKGREY;
|
2012-01-11 09:04:04 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
out.color(color);
|
|
|
|
out << prefix << "Constraint " << flush;
|
2012-08-18 23:21:25 -06:00
|
|
|
out.color(COLOR_GREY);
|
2012-03-10 04:55:42 -07:00
|
|
|
out << cv->config.val() << " " << flush;
|
|
|
|
out.color(color);
|
|
|
|
out << (cv->goalByCount() ? "count " : "amount ")
|
2012-01-10 06:23:37 -07:00
|
|
|
<< cv->goalCount() << " (gap " << cv->goalGap() << ")" << endl;
|
2012-03-10 04:55:42 -07:00
|
|
|
out.reset_color();
|
2012-01-10 06:23:37 -07:00
|
|
|
|
|
|
|
if (cv->item_count || cv->item_inuse)
|
2012-03-10 04:55:42 -07:00
|
|
|
out << prefix << " items: amount " << cv->item_amount << "; "
|
2012-01-10 06:23:37 -07:00
|
|
|
<< cv->item_count << " stacks available, "
|
|
|
|
<< cv->item_inuse << " in use." << endl;
|
|
|
|
|
|
|
|
if (no_job) return;
|
|
|
|
|
|
|
|
if (cv->jobs.empty())
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr(" (no jobs)\n");
|
2012-01-10 06:23:37 -07:00
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
std::vector<ProtectedJob*> unique_jobs;
|
|
|
|
std::vector<int> unique_counts;
|
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < cv->jobs.size(); i++)
|
2012-01-10 06:23:37 -07:00
|
|
|
{
|
|
|
|
ProtectedJob *pj = cv->jobs[i];
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t j = 0; j < unique_jobs.size(); j++)
|
2012-01-11 09:04:04 -07:00
|
|
|
{
|
|
|
|
if (unique_jobs[j]->building_id == pj->building_id &&
|
|
|
|
*unique_jobs[j]->actual_job == *pj->actual_job)
|
|
|
|
{
|
|
|
|
unique_counts[j]++;
|
|
|
|
goto next_job;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unique_jobs.push_back(pj);
|
|
|
|
unique_counts.push_back(1);
|
|
|
|
next_job:;
|
|
|
|
}
|
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < unique_jobs.size(); i++)
|
2012-01-11 09:04:04 -07:00
|
|
|
{
|
|
|
|
ProtectedJob *pj = unique_jobs[i];
|
2012-01-10 06:23:37 -07:00
|
|
|
df::job *job = pj->actual_job;
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
std::string start = prefix + " " + shortJobDescription(job);
|
2012-01-10 06:23:37 -07:00
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
if (!pj->isActuallyResumed())
|
|
|
|
{
|
|
|
|
if (pj->want_resumed)
|
|
|
|
{
|
2012-08-18 23:21:25 -06:00
|
|
|
out.color(COLOR_YELLOW);
|
2012-03-10 04:55:42 -07:00
|
|
|
out << start << " (delayed)" << endl;
|
2012-01-11 09:04:04 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-18 23:21:25 -06:00
|
|
|
out.color(COLOR_BLUE);
|
2012-03-10 04:55:42 -07:00
|
|
|
out << start << " (suspended)" << endl;
|
2012-01-11 09:04:04 -07:00
|
|
|
}
|
|
|
|
}
|
2012-01-10 06:23:37 -07:00
|
|
|
else
|
2012-01-11 09:04:04 -07:00
|
|
|
{
|
2012-08-18 23:21:25 -06:00
|
|
|
out.color(COLOR_GREEN);
|
2012-03-10 04:55:42 -07:00
|
|
|
out << start << endl;
|
2012-01-11 09:04:04 -07:00
|
|
|
}
|
2012-01-10 06:23:37 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
out.reset_color();
|
2012-01-10 06:23:37 -07:00
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
if (unique_counts[i] > 1)
|
2012-03-10 04:55:42 -07:00
|
|
|
out << prefix << " (" << unique_counts[i] << " copies)" << endl;
|
2012-01-10 06:23:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void print_job(color_ostream &out, ProtectedJob *pj)
|
2012-01-10 06:23:37 -07:00
|
|
|
{
|
|
|
|
if (!pj)
|
|
|
|
return;
|
|
|
|
|
2012-01-12 09:07:53 -07:00
|
|
|
df::job *job = pj->isLive() ? pj->actual_job : pj->job_copy;
|
|
|
|
|
2012-04-10 01:43:36 -06:00
|
|
|
Job::printJobDetails(out, job);
|
2012-01-12 09:07:53 -07:00
|
|
|
|
|
|
|
if (job->job_type == job_type::MeltMetalObject &&
|
|
|
|
isOptionEnabled(CF_AUTOMELT))
|
|
|
|
{
|
|
|
|
if (meltable_count <= 0)
|
2012-08-18 23:21:25 -06:00
|
|
|
out.color(COLOR_CYAN);
|
2012-01-12 09:07:53 -07:00
|
|
|
else if (pj->want_resumed && !pj->isActuallyResumed())
|
2012-08-18 23:21:25 -06:00
|
|
|
out.color(COLOR_YELLOW);
|
2012-01-12 09:07:53 -07:00
|
|
|
else
|
2012-08-18 23:21:25 -06:00
|
|
|
out.color(COLOR_GREEN);
|
2012-03-10 04:55:42 -07:00
|
|
|
out << " Meltable: " << meltable_count << " objects." << endl;
|
|
|
|
out.reset_color();
|
2012-01-12 09:07:53 -07:00
|
|
|
}
|
2012-01-10 06:23:37 -07:00
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < pj->constraints.size(); i++)
|
2012-03-10 04:55:42 -07:00
|
|
|
print_constraint(out, pj->constraints[i], true, " ");
|
2012-01-10 06:23:37 -07:00
|
|
|
}
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
/******************************
|
|
|
|
* Inventory Monitor *
|
|
|
|
******************************/
|
|
|
|
#define MAX_ITEM_NAME 17
|
|
|
|
#define MAX_MASK 10
|
|
|
|
#define MAX_MATERIAL 21
|
|
|
|
|
|
|
|
#define SIDEBAR_WIDTH 30
|
|
|
|
#define COLOR_TITLE COLOR_BLUE
|
|
|
|
#define COLOR_UNSELECTED COLOR_GREY
|
|
|
|
#define COLOR_SELECTED COLOR_WHITE
|
|
|
|
#define COLOR_HIGHLIGHTED COLOR_GREEN
|
|
|
|
|
|
|
|
namespace wf_ui
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
/*
|
|
|
|
* Utility Functions
|
|
|
|
*/
|
|
|
|
typedef int8_t UIColor;
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
const int ascii_to_enum_offset = interface_key::STRING_A048 - '0';
|
2012-01-28 05:03:56 -07:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
inline string int_to_string(const int n)
|
|
|
|
{
|
|
|
|
return static_cast<ostringstream*>( &(ostringstream() << n) )->str();
|
2012-01-09 08:20:28 -07:00
|
|
|
}
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
static void set_to_limit(int &value, const int maximum, const int min = 0)
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
if (value < min)
|
|
|
|
value = min;
|
|
|
|
else if (value > maximum)
|
|
|
|
value = maximum;
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
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);
|
|
|
|
}
|
2012-01-09 06:29:28 -07:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
static string pad_string(string text, const int size, const bool front = true, const bool trim = false)
|
2012-01-09 06:29:28 -07:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
if (text.length() > size)
|
2012-01-09 06:29:28 -07:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
if (trim && size > 10)
|
2012-01-12 09:07:53 -07:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
text = text.substr(0, size-3);
|
|
|
|
text.append("...");
|
2012-01-12 09:07:53 -07:00
|
|
|
}
|
2012-11-05 11:48:24 -07:00
|
|
|
return text;
|
2012-01-09 06:29:28 -07:00
|
|
|
}
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
string aligned(size - text.length(), ' ');
|
|
|
|
if (front)
|
2012-01-09 06:29:28 -07:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
aligned.append(text);
|
|
|
|
return aligned;
|
2012-01-09 06:29:28 -07:00
|
|
|
}
|
2012-01-12 09:07:53 -07:00
|
|
|
else
|
2012-11-05 11:48:24 -07:00
|
|
|
{
|
|
|
|
text.append(aligned);
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
}
|
2012-01-12 09:07:53 -07:00
|
|
|
|
2012-11-29 03:15:24 -07:00
|
|
|
static int get_left_margin()
|
|
|
|
{
|
2013-01-02 02:27:26 -07:00
|
|
|
int left_margin = gps->dimx - 30;
|
|
|
|
int8_t a = *df::global::ui_menu_width;
|
|
|
|
int8_t b = *df::global::ui_area_map_width;
|
|
|
|
if ((a == 1 && b > 1) || (a == 2 && b == 2))
|
|
|
|
left_margin -= 24;
|
|
|
|
|
|
|
|
return left_margin;
|
2012-11-29 03:15:24 -07:00
|
|
|
}
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
/*
|
|
|
|
* Adjustment Dialog
|
|
|
|
*/
|
2012-01-12 09:07:53 -07:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
class AdjustmentScreen
|
2012-01-09 10:25:55 -07:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
public:
|
|
|
|
int32_t x, y, left_margin;
|
2012-01-09 06:29:28 -07:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
AdjustmentScreen();
|
|
|
|
void reset();
|
|
|
|
bool feed(set<df::interface_key> *input, ItemConstraint *cv, ProtectedJob *pj = NULL);
|
2012-11-29 03:15:24 -07:00
|
|
|
void render(ItemConstraint *cv, bool in_monitor);
|
2012-01-09 06:29:28 -07:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
protected:
|
|
|
|
int32_t adjustment_ui_display_start;
|
|
|
|
|
|
|
|
virtual void onConstraintChanged() {}
|
|
|
|
virtual void onModeChanged() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool edit_limit, edit_gap;
|
|
|
|
string edit_string;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
AdjustmentScreen::AdjustmentScreen()
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
reset();
|
|
|
|
}
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
void AdjustmentScreen::reset()
|
|
|
|
{
|
|
|
|
edit_gap = false;
|
|
|
|
edit_limit = false;
|
|
|
|
adjustment_ui_display_start = 24;
|
|
|
|
}
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
bool AdjustmentScreen::feed(set<df::interface_key> *input, ItemConstraint *cv, ProtectedJob *pj /* = NULL */)
|
|
|
|
{
|
2013-01-03 02:01:01 -07:00
|
|
|
if (input->count(interface_key::CUSTOM_T) && !edit_limit && !edit_gap)
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
2013-01-03 02:01:01 -07:00
|
|
|
if (!cv)
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
2013-01-03 02:01:01 -07:00
|
|
|
// Add tracking
|
|
|
|
return false;
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
|
|
|
|
2013-01-03 02:01:01 -07:00
|
|
|
// Remove tracking
|
|
|
|
if (pj)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2013-01-03 02:01:01 -07:00
|
|
|
for (vector<ItemConstraint*>::iterator it = pj->constraints.begin(); it < pj->constraints.end(); it++)
|
|
|
|
delete_constraint(*it);
|
2012-10-24 05:09:34 -06:00
|
|
|
|
2013-01-03 02:01:01 -07:00
|
|
|
forget_job(color_ostream_proxy(Core::getInstance().getConsole()), pj);
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
2013-01-03 02:01:01 -07:00
|
|
|
else
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2013-01-03 02:01:01 -07:00
|
|
|
delete_constraint(cv);
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
|
2013-01-03 02:01:01 -07:00
|
|
|
onConstraintChanged();
|
2012-10-24 05:09:34 -06:00
|
|
|
return true;
|
|
|
|
}
|
2013-01-03 02:01:01 -07:00
|
|
|
|
|
|
|
if (cv)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2013-01-03 02:01:01 -07:00
|
|
|
if (edit_limit || edit_gap)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2013-01-03 02:01:01 -07:00
|
|
|
df::interface_key last_token = *input->rbegin();
|
|
|
|
if (last_token == interface_key::STRING_A000)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2013-01-03 02:01:01 -07:00
|
|
|
// Backspace
|
|
|
|
if (edit_string.length() > 0)
|
|
|
|
{
|
|
|
|
edit_string.erase(edit_string.length()-1);
|
|
|
|
}
|
2012-10-24 05:09:34 -06:00
|
|
|
|
2013-01-03 02:01:01 -07:00
|
|
|
return true;
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
2013-01-03 02:01:01 -07:00
|
|
|
|
|
|
|
if (edit_string.length() >= 6)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (last_token >= interface_key::STRING_A048 && last_token <= interface_key::STRING_A057)
|
|
|
|
{
|
|
|
|
// Numeric character
|
|
|
|
edit_string += last_token - ascii_to_enum_offset;
|
|
|
|
}
|
|
|
|
else if (input->count(interface_key::SELECT) || input->count(interface_key::LEAVESCREEN))
|
|
|
|
{
|
|
|
|
if (input->count(interface_key::SELECT) && edit_string.length() > 0)
|
|
|
|
{
|
|
|
|
if (edit_limit)
|
|
|
|
cv->setGoalCount(atoi(edit_string.c_str()));
|
|
|
|
else
|
|
|
|
cv->setGoalGap(atoi(edit_string.c_str()));
|
|
|
|
|
|
|
|
onConstraintChanged();
|
|
|
|
}
|
|
|
|
edit_string.clear();
|
|
|
|
edit_limit = false;
|
|
|
|
edit_gap = false;
|
|
|
|
}
|
|
|
|
else if (last_token == interface_key::STRING_A000)
|
|
|
|
{
|
|
|
|
// Backspace
|
|
|
|
if (edit_string.length() > 0)
|
|
|
|
{
|
|
|
|
edit_string.erase(edit_string.length()-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
2013-01-03 02:01:01 -07:00
|
|
|
else if (input->count(interface_key::CUSTOM_L))
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2013-01-03 02:01:01 -07:00
|
|
|
edit_string = int_to_string(cv->goalCount());
|
|
|
|
edit_limit = true;
|
|
|
|
}
|
|
|
|
else if (input->count(interface_key::CUSTOM_G))
|
|
|
|
{
|
|
|
|
edit_string = int_to_string(cv->goalGap());
|
|
|
|
edit_gap = true;
|
|
|
|
}
|
|
|
|
else if (input->count(interface_key::CUSTOM_N))
|
|
|
|
{
|
|
|
|
cv->setGoalByCount(!cv->goalByCount());
|
|
|
|
onModeChanged();
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-03 02:01:01 -07:00
|
|
|
return false;
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
|
2012-11-29 03:15:24 -07:00
|
|
|
void AdjustmentScreen::render(ItemConstraint *cv, bool in_monitor)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2012-11-29 03:15:24 -07:00
|
|
|
left_margin = (in_monitor) ? gps->dimx - 30 : get_left_margin();
|
2012-10-24 05:09:34 -06:00
|
|
|
x = left_margin;
|
|
|
|
y = adjustment_ui_display_start;
|
2012-11-05 11:48:24 -07:00
|
|
|
OutputString(COLOR_BROWN, x, y, "Workflow Settings", true, left_margin);
|
2012-10-24 05:09:34 -06:00
|
|
|
if (cv != NULL)
|
|
|
|
{
|
|
|
|
string text;
|
|
|
|
text.reserve(20);
|
|
|
|
|
|
|
|
text.append("Available: ");
|
|
|
|
text.append(int_to_string((cv->goalByCount()) ? cv->item_count : cv->item_amount));
|
|
|
|
text.append(" ");
|
|
|
|
text.append((cv->goalByCount()) ? "Stacks" : "Items");
|
|
|
|
|
|
|
|
OutputString(15, x, y, text, true, left_margin);
|
|
|
|
|
|
|
|
text.clear();
|
|
|
|
text.append("In use : ");
|
|
|
|
text.append(int_to_string(cv->item_inuse));
|
|
|
|
OutputString(15, x, y, text, true, left_margin);
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
++y;
|
2012-10-24 05:09:34 -06:00
|
|
|
text.clear();
|
2012-11-05 11:48:24 -07:00
|
|
|
text.append("Limit : ");
|
2012-10-24 05:09:34 -06:00
|
|
|
text.append((edit_limit) ? edit_string : int_to_string(cv->goalCount()));
|
|
|
|
OutputHotkeyString(x, y, text.c_str(), "l");
|
|
|
|
if (edit_limit)
|
|
|
|
OutputString(10, x, y, "_");
|
|
|
|
|
|
|
|
++y;
|
|
|
|
x = left_margin;
|
|
|
|
text.clear();
|
2012-11-05 11:48:24 -07:00
|
|
|
text.append("Gap : ");
|
2012-10-24 05:09:34 -06:00
|
|
|
text.append((edit_gap) ? edit_string : int_to_string(cv->goalGap()));
|
|
|
|
OutputHotkeyString(x, y, text.c_str(), "g");
|
|
|
|
if (edit_gap)
|
|
|
|
OutputString(10, x, y, "_");
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
++y;
|
|
|
|
x = left_margin;
|
|
|
|
OutputHotkeyString(x, y, "Disable Tracking", "t", true, left_margin);
|
|
|
|
OutputHotkeyString(x, y, (cv->goalByCount()) ? "Count Items" : "Count Stacks", "n");
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
OutputHotkeyString(x, y, "Enable Tracking", "t", true, left_margin);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* List classes
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class ListEntry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
T elem;
|
|
|
|
string text;
|
|
|
|
bool selected;
|
|
|
|
|
|
|
|
ListEntry(string text, T elem)
|
|
|
|
{
|
|
|
|
this->text = text;
|
|
|
|
this->elem = elem;
|
|
|
|
selected = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class ListColumn
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
string title;
|
|
|
|
int highlighted_index;
|
|
|
|
int display_start_offset;
|
2012-11-05 11:48:24 -07:00
|
|
|
int32_t bottom_margin, search_margin, left_margin;
|
|
|
|
bool search_entry_mode;
|
2012-10-24 05:09:34 -06:00
|
|
|
bool multiselect;
|
|
|
|
bool allow_null;
|
2012-11-05 11:48:24 -07:00
|
|
|
bool auto_select;
|
|
|
|
bool force_sort;
|
2012-10-24 05:09:34 -06:00
|
|
|
|
|
|
|
ListColumn()
|
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
clear();
|
|
|
|
left_margin = 2;
|
|
|
|
bottom_margin = 3;
|
|
|
|
search_margin = 38;
|
2012-10-24 05:09:34 -06:00
|
|
|
highlighted_index = 0;
|
|
|
|
multiselect = false;
|
|
|
|
allow_null = true;
|
2012-11-05 11:48:24 -07:00
|
|
|
auto_select = false;
|
|
|
|
search_entry_mode = false;
|
|
|
|
force_sort = false;
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
list.clear();
|
2012-11-05 11:48:24 -07:00
|
|
|
display_list.clear();
|
|
|
|
display_start_offset = 0;
|
|
|
|
max_item_width = 0;
|
|
|
|
resize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void resize()
|
|
|
|
{
|
|
|
|
display_max_rows = gps->dimy - 4 - bottom_margin;
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void add(ListEntry<T> &entry)
|
|
|
|
{
|
|
|
|
list.push_back(entry);
|
2012-11-05 11:48:24 -07:00
|
|
|
if (entry.text.length() > max_item_width)
|
|
|
|
max_item_width = entry.text.length();
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
|
2012-11-10 23:42:30 -07:00
|
|
|
void add(const string &text, T &elem)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
|
|
|
list.push_back(ListEntry<T>(text, elem));
|
2012-11-05 11:48:24 -07:00
|
|
|
if (text.length() > max_item_width)
|
|
|
|
max_item_width = text.length();
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
virtual void display_extras(const T &elem, int32_t &x, int32_t &y) const {}
|
2012-10-24 05:09:34 -06:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
void display(const bool is_selected_column) const
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
int32_t y = 2;
|
2012-10-24 05:09:34 -06:00
|
|
|
paint_text(COLOR_TITLE, left_margin, y, title);
|
|
|
|
|
|
|
|
int last_index_able_to_display = display_start_offset + display_max_rows;
|
2012-11-05 11:48:24 -07:00
|
|
|
for (int i = display_start_offset; i < display_list.size() && i < last_index_able_to_display; i++)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
|
|
|
++y;
|
2012-11-05 11:48:24 -07:00
|
|
|
UIColor fg_color = (display_list[i]->selected) ? COLOR_SELECTED : COLOR_UNSELECTED;
|
2012-10-24 05:09:34 -06:00
|
|
|
UIColor bg_color = (is_selected_column && i == highlighted_index) ? COLOR_HIGHLIGHTED : COLOR_BLACK;
|
2012-11-05 11:48:24 -07:00
|
|
|
paint_text(fg_color, left_margin, y, display_list[i]->text, bg_color);
|
|
|
|
int x = left_margin + display_list[i]->text.length() + 1;
|
|
|
|
display_extras(display_list[i]->elem, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_selected_column)
|
|
|
|
{
|
|
|
|
y = gps->dimy - bottom_margin;
|
|
|
|
int32_t x = search_margin;
|
|
|
|
OutputHotkeyString(x, y, "Search" ,"S");
|
|
|
|
if (!search_string.empty() || search_entry_mode)
|
|
|
|
{
|
|
|
|
OutputString(COLOR_WHITE, x, y, ": ");
|
|
|
|
OutputString(COLOR_WHITE, x, y, search_string);
|
|
|
|
if (search_entry_mode)
|
|
|
|
OutputString(COLOR_LIGHTGREEN, x, y, "_");
|
|
|
|
}
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
void filter_display()
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
ListEntry<T> *prev_selected = (getDisplayListSize() > 0) ? display_list[highlighted_index] : NULL;
|
|
|
|
display_list.clear();
|
|
|
|
for (size_t i = 0; i < list.size(); i++)
|
|
|
|
{
|
|
|
|
if (search_string.empty() || list[i].text.find(search_string) != string::npos)
|
|
|
|
{
|
|
|
|
ListEntry<T> *entry = &list[i];
|
|
|
|
display_list.push_back(entry);
|
|
|
|
if (entry == prev_selected)
|
|
|
|
highlighted_index = display_list.size() - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
changeHighlight(0);
|
|
|
|
}
|
2012-10-24 05:09:34 -06:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
void selectDefaultEntry()
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < display_list.size(); i++)
|
|
|
|
{
|
|
|
|
if (display_list[i]->selected)
|
|
|
|
{
|
|
|
|
highlighted_index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-24 05:09:34 -06:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
void validateHighlight()
|
|
|
|
{
|
|
|
|
set_to_limit(highlighted_index, display_list.size() - 1);
|
2012-10-24 05:09:34 -06:00
|
|
|
|
|
|
|
if (highlighted_index < display_start_offset)
|
|
|
|
display_start_offset = highlighted_index;
|
|
|
|
else if (highlighted_index >= display_start_offset + display_max_rows)
|
|
|
|
display_start_offset = highlighted_index - display_max_rows + 1;
|
2012-11-05 11:48:24 -07:00
|
|
|
|
|
|
|
if (auto_select || (!allow_null && list.size() == 1))
|
|
|
|
display_list[highlighted_index]->selected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void changeHighlight(const int highlight_change, const int offset_shift = 0)
|
|
|
|
{
|
|
|
|
if (!initHighlightChange())
|
|
|
|
return;
|
|
|
|
|
|
|
|
highlighted_index += highlight_change + offset_shift * display_max_rows;
|
|
|
|
|
|
|
|
display_start_offset += offset_shift * display_max_rows;
|
|
|
|
set_to_limit(display_start_offset, max(0, (int)(display_list.size())-display_max_rows));
|
|
|
|
validateHighlight();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setHighlight(const int index)
|
|
|
|
{
|
|
|
|
if (!initHighlightChange())
|
|
|
|
return;
|
|
|
|
|
|
|
|
highlighted_index = index;
|
|
|
|
validateHighlight();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool initHighlightChange()
|
|
|
|
{
|
|
|
|
if (display_list.size() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (auto_select && !multiselect)
|
|
|
|
{
|
2012-11-10 23:42:30 -07:00
|
|
|
for (typename vector< ListEntry<T> >::iterator it = list.begin(); it != list.end(); it++)
|
2012-11-05 11:48:24 -07:00
|
|
|
{
|
|
|
|
it->selected = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void toggleHighlighted()
|
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
if (auto_select)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ListEntry<T> *entry = display_list[highlighted_index];
|
2012-10-24 05:09:34 -06:00
|
|
|
if (!multiselect || !allow_null)
|
|
|
|
{
|
|
|
|
int selected_count = 0;
|
|
|
|
for (size_t i = 0; i < list.size(); i++)
|
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
if (!multiselect && !entry->selected)
|
2012-10-24 05:09:34 -06:00
|
|
|
list[i].selected = false;
|
|
|
|
if (!allow_null && list[i].selected)
|
|
|
|
selected_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!allow_null && entry->selected && selected_count == 1)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->selected = !entry->selected;
|
|
|
|
}
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
vector<T*> getSelectedElems(bool only_one = false)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
vector<T*> results;
|
2012-11-10 23:42:30 -07:00
|
|
|
for (typename vector< ListEntry<T> >::iterator it = list.begin(); it != list.end(); it++)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
|
|
|
if ((*it).selected)
|
2012-11-05 11:48:24 -07:00
|
|
|
{
|
|
|
|
results.push_back(&(*it).elem);
|
|
|
|
if (only_one)
|
|
|
|
break;
|
|
|
|
}
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
T* getFirstSelectedElem()
|
|
|
|
{
|
|
|
|
vector<T*> results = getSelectedElems(true);
|
|
|
|
if (results.size() == 0)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return results[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getDisplayListSize()
|
|
|
|
{
|
|
|
|
return display_list.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getBaseListSize()
|
|
|
|
{
|
|
|
|
return list.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool feed(set<df::interface_key> *input)
|
|
|
|
{
|
|
|
|
if (input->count(interface_key::CURSOR_UP))
|
|
|
|
{
|
|
|
|
search_entry_mode = false;
|
|
|
|
changeHighlight(-1);
|
|
|
|
}
|
|
|
|
else if (input->count(interface_key::CURSOR_DOWN))
|
|
|
|
{
|
|
|
|
search_entry_mode = false;
|
|
|
|
changeHighlight(1);
|
|
|
|
}
|
|
|
|
else if (input->count(interface_key::STANDARDSCROLL_PAGEUP))
|
|
|
|
{
|
|
|
|
search_entry_mode = false;
|
|
|
|
changeHighlight(0, -1);
|
|
|
|
}
|
|
|
|
else if (input->count(interface_key::STANDARDSCROLL_PAGEDOWN))
|
|
|
|
{
|
|
|
|
search_entry_mode = false;
|
|
|
|
changeHighlight(0, 1);
|
|
|
|
}
|
|
|
|
else if (search_entry_mode)
|
|
|
|
{
|
|
|
|
// Search query typing mode
|
|
|
|
|
|
|
|
df::interface_key last_token = *input->rbegin();
|
|
|
|
if (last_token >= interface_key::STRING_A032 && last_token <= interface_key::STRING_A126)
|
|
|
|
{
|
|
|
|
// Standard character
|
|
|
|
search_string += last_token - ascii_to_enum_offset;
|
|
|
|
filter_display();
|
|
|
|
}
|
|
|
|
else if (last_token == interface_key::STRING_A000)
|
|
|
|
{
|
|
|
|
// Backspace
|
|
|
|
if (search_string.length() > 0)
|
|
|
|
{
|
|
|
|
search_string.erase(search_string.length()-1);
|
|
|
|
filter_display();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (input->count(interface_key::SELECT) || input->count(interface_key::LEAVESCREEN))
|
|
|
|
{
|
|
|
|
// ENTER or ESC: leave typing mode
|
|
|
|
search_entry_mode = false;
|
|
|
|
}
|
|
|
|
else if (input->count(interface_key::CURSOR_LEFT) || input->count(interface_key::CURSOR_RIGHT))
|
|
|
|
{
|
|
|
|
// Arrow key pressed. Leave entry mode and allow screen to process key
|
|
|
|
search_entry_mode = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not in search query typing mode
|
|
|
|
else if (input->count(interface_key::SELECT) && !auto_select)
|
|
|
|
{
|
|
|
|
toggleHighlighted();
|
|
|
|
}
|
|
|
|
else if (input->count(interface_key::CUSTOM_S))
|
|
|
|
{
|
|
|
|
search_entry_mode = true;
|
|
|
|
}
|
|
|
|
else if (input->count(interface_key::CUSTOM_SHIFT_S))
|
|
|
|
{
|
|
|
|
search_string.clear();
|
|
|
|
filter_display();
|
|
|
|
}
|
|
|
|
else if (enabler->tracking_on && gps->mouse_x != -1 && gps->mouse_y != -1 && enabler->mouse_lbut)
|
|
|
|
{
|
|
|
|
return setHighlightByMouse();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool setHighlightByMouse()
|
|
|
|
{
|
|
|
|
if (gps->mouse_y >= 3 && gps->mouse_y < display_max_rows + 3 &&
|
|
|
|
gps->mouse_x >= left_margin && gps->mouse_x < left_margin + max_item_width)
|
|
|
|
{
|
|
|
|
int new_index = display_start_offset + gps->mouse_y - 3;
|
|
|
|
if (new_index < display_list.size())
|
|
|
|
setHighlight(new_index);
|
|
|
|
|
|
|
|
enabler->mouse_lbut = enabler->mouse_rbut = 0;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool compareText(ListEntry<T> const& a, ListEntry<T> const& b)
|
|
|
|
{
|
|
|
|
return a.text.compare(b.text) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void doSort(bool (*function)(ListEntry<T> const&, ListEntry<T> const&))
|
|
|
|
{
|
|
|
|
if (force_sort || list.size() < 100)
|
|
|
|
std::sort(list.begin(), list.end(), function);
|
|
|
|
|
|
|
|
filter_display();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void sort()
|
|
|
|
{
|
|
|
|
doSort(&compareText);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
vector< ListEntry<T> > list;
|
|
|
|
vector< ListEntry<T>* > display_list;
|
|
|
|
string search_string;
|
|
|
|
int display_max_rows;
|
|
|
|
int max_item_width;
|
|
|
|
};
|
2012-10-24 05:09:34 -06:00
|
|
|
|
|
|
|
class viewscreenChooseMaterial : public dfhack_viewscreen
|
|
|
|
{
|
|
|
|
public:
|
2012-11-05 11:48:24 -07:00
|
|
|
static bool reset_list;
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
viewscreenChooseMaterial(ItemConstraint *cv = NULL);
|
|
|
|
void feed(set<df::interface_key> *input);
|
|
|
|
void render();
|
|
|
|
|
|
|
|
std::string getFocusString() { return "wfchoosemat"; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
ItemConstraint *cv;
|
|
|
|
|
|
|
|
ListColumn<ItemTypeInfo> items_column;
|
|
|
|
ListColumn<df::dfhack_material_category> masks_column;
|
|
|
|
ListColumn<MaterialInfo> materials_column;
|
|
|
|
vector< ListEntry<df::dfhack_material_category> > all_masks;
|
|
|
|
|
|
|
|
int selected_column;
|
|
|
|
|
|
|
|
void populateItems();
|
|
|
|
void populateMasks(const bool set_defaults = false);
|
|
|
|
void populateMaterials(const bool set_defaults = false);
|
|
|
|
|
|
|
|
bool addMaterialEntry(df::dfhack_material_category &selected_category,
|
|
|
|
MaterialInfo &material, string name, const bool set_defaults);
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
virtual void resize(int32_t x, int32_t y);
|
2012-10-24 05:09:34 -06:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
void validateColumn();
|
2012-10-24 05:09:34 -06:00
|
|
|
};
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
bool viewscreenChooseMaterial::reset_list = false;
|
|
|
|
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
viewscreenChooseMaterial::viewscreenChooseMaterial(ItemConstraint *cv /*= NULL*/)
|
|
|
|
{
|
|
|
|
this->cv = cv;
|
|
|
|
selected_column = 0;
|
|
|
|
items_column.title = "Item";
|
|
|
|
items_column.allow_null = false;
|
2012-11-05 11:48:24 -07:00
|
|
|
items_column.multiselect = false;
|
|
|
|
items_column.auto_select = true;
|
|
|
|
items_column.force_sort = true;
|
2012-10-24 05:09:34 -06:00
|
|
|
masks_column.title = "Type";
|
|
|
|
masks_column.multiselect = true;
|
2012-11-05 11:48:24 -07:00
|
|
|
masks_column.left_margin = MAX_ITEM_NAME + 3;
|
|
|
|
materials_column.left_margin = MAX_ITEM_NAME + MAX_MASK + 4;
|
2012-10-24 05:09:34 -06:00
|
|
|
materials_column.title = "Material";
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
populateItems();
|
2012-11-05 11:48:24 -07:00
|
|
|
items_column.changeHighlight(0);
|
2012-10-24 05:09:34 -06:00
|
|
|
|
|
|
|
vector<string> raw_masks;
|
|
|
|
df::dfhack_material_category full_mat_mask, curr_mat_mask;
|
|
|
|
full_mat_mask.whole = -1;
|
|
|
|
curr_mat_mask.whole = 1;
|
|
|
|
bitfield_to_string(&raw_masks, full_mat_mask);
|
|
|
|
for (int i = 0; i < raw_masks.size(); i++)
|
|
|
|
{
|
|
|
|
if (raw_masks[i][0] == '?')
|
|
|
|
break;
|
|
|
|
|
|
|
|
all_masks.push_back(ListEntry<df::dfhack_material_category>(pad_string(raw_masks[i], MAX_MASK, false), curr_mat_mask));
|
|
|
|
curr_mat_mask.whole <<= 1;
|
|
|
|
}
|
|
|
|
populateMasks(cv != NULL);
|
|
|
|
populateMaterials(cv != NULL);
|
2012-11-05 11:48:24 -07:00
|
|
|
|
|
|
|
masks_column.selectDefaultEntry();
|
|
|
|
materials_column.selectDefaultEntry();
|
|
|
|
materials_column.changeHighlight(0);
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void viewscreenChooseMaterial::populateItems()
|
|
|
|
{
|
|
|
|
items_column.clear();
|
|
|
|
if (cv != NULL)
|
|
|
|
{
|
|
|
|
items_column.add(cv->item.toString(), cv->item);
|
|
|
|
}
|
2012-11-05 11:48:24 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
typedef df::enum_traits<df::item_type> traits;
|
|
|
|
int size = traits::last_item_value-traits::first_item_value+1;
|
|
|
|
for (size_t i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
//string item_name = traits::key_table[i];
|
|
|
|
df::world_raws::T_itemdefs &defs = df::global::world->raws.itemdefs;
|
|
|
|
df::item_type val = df::item_type(i);
|
|
|
|
switch (val)
|
|
|
|
{
|
|
|
|
#define ITEM(type_string,vec,tclass) \
|
|
|
|
case df::item_type::type_string: \
|
|
|
|
for (size_t j = 0; j < defs.vec.size(); j++) \
|
|
|
|
{ \
|
|
|
|
if (defs.vec[j]->name[0] == '?') \
|
|
|
|
continue; \
|
|
|
|
DFHack::ItemTypeInfo item; \
|
|
|
|
item.type = (df::item_type) i; \
|
|
|
|
item.subtype = j; \
|
|
|
|
item.custom = defs.vec[j]; \
|
|
|
|
items_column.add(defs.vec[j]->name, item); \
|
|
|
|
} \
|
|
|
|
break;
|
|
|
|
|
|
|
|
ITEMDEF_VECTORS
|
|
|
|
#undef ITEM
|
|
|
|
default:
|
|
|
|
DFHack::ItemTypeInfo item;
|
|
|
|
item.type = (df::item_type) i;
|
|
|
|
string name = item.toString();
|
|
|
|
if (name[0] != '?')
|
|
|
|
items_column.add(name, item);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
items_column.sort();
|
|
|
|
items_column.changeHighlight(0);
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void viewscreenChooseMaterial::populateMasks(const bool set_defaults /*= false */)
|
|
|
|
{
|
|
|
|
masks_column.clear();
|
|
|
|
for (vector< ListEntry<df::dfhack_material_category> >::iterator it = all_masks.begin(); it != all_masks.end(); it++)
|
|
|
|
{
|
|
|
|
auto entry = *it;
|
|
|
|
if (set_defaults)
|
|
|
|
{
|
|
|
|
if (cv->mat_mask.whole & entry.elem.whole)
|
|
|
|
entry.selected = true;
|
|
|
|
}
|
|
|
|
masks_column.add(entry);
|
|
|
|
}
|
2012-11-05 11:48:24 -07:00
|
|
|
masks_column.sort();
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void viewscreenChooseMaterial::populateMaterials(const bool set_defaults /*= false */)
|
|
|
|
{
|
|
|
|
materials_column.clear();
|
|
|
|
df::dfhack_material_category selected_category;
|
2012-11-05 11:48:24 -07:00
|
|
|
vector<df::dfhack_material_category *> selected_materials = masks_column.getSelectedElems();
|
2012-10-24 05:09:34 -06:00
|
|
|
if (selected_materials.size() == 1)
|
2012-11-05 11:48:24 -07:00
|
|
|
selected_category = *selected_materials[0];
|
2012-10-24 05:09:34 -06:00
|
|
|
else if (selected_materials.size() > 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
df::world_raws &raws = world->raws;
|
|
|
|
for (int i = 1; i < DFHack::MaterialInfo::NUM_BUILTIN; i++)
|
|
|
|
{
|
|
|
|
auto obj = raws.mat_table.builtin[i];
|
|
|
|
if (obj)
|
|
|
|
{
|
|
|
|
MaterialInfo material;
|
|
|
|
material.decode(i, -1);
|
|
|
|
addMaterialEntry(selected_category, material, material.toString(), set_defaults);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < raws.inorganics.size(); i++)
|
|
|
|
{
|
|
|
|
df::inorganic_raw *p = raws.inorganics[i];
|
|
|
|
MaterialInfo material;
|
|
|
|
material.decode(0, i);
|
|
|
|
addMaterialEntry(selected_category, material, material.toString(), set_defaults);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < raws.plants.all.size(); i++)
|
|
|
|
{
|
|
|
|
df::plant_raw *p = raws.plants.all[i];
|
|
|
|
string basename = p->name;
|
|
|
|
|
|
|
|
MaterialInfo material;
|
|
|
|
material.decode(p->material_defs.type_basic_mat, p->material_defs.idx_basic_mat);
|
|
|
|
if (!selected_category.whole || material.matches(selected_category))
|
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
if (p->material.size() > 1)
|
|
|
|
basename.append(" (all)");
|
|
|
|
|
|
|
|
ListEntry<MaterialInfo> entry(pad_string(basename, MAX_MATERIAL, false), material);
|
2012-10-24 05:09:34 -06:00
|
|
|
if (set_defaults)
|
|
|
|
{
|
|
|
|
if (cv->material.matches(material))
|
|
|
|
entry.selected = true;
|
|
|
|
}
|
|
|
|
materials_column.add(entry);
|
|
|
|
|
|
|
|
for (size_t j = 0; p->material.size() > 1 && j < p->material.size(); j++)
|
|
|
|
{
|
|
|
|
MaterialInfo material;
|
|
|
|
material.decode(DFHack::MaterialInfo::PLANT_BASE+j, i);
|
2012-11-05 11:48:24 -07:00
|
|
|
//if (addMaterialEntry(selected_category, material, basename+" (" + p->material[j]->id + "): " + material.toString(), set_defaults))
|
|
|
|
if (addMaterialEntry(selected_category, material, material.toString(), set_defaults))
|
2012-10-24 05:09:34 -06:00
|
|
|
entry.selected = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < raws.creatures.all.size(); i++)
|
|
|
|
{
|
|
|
|
df::creature_raw *p = raws.creatures.all[i];
|
|
|
|
string basename = p->name[0];
|
|
|
|
|
|
|
|
for (size_t j = 0; j < p->material.size(); j++)
|
|
|
|
{
|
|
|
|
MaterialInfo material;
|
|
|
|
material.decode(DFHack::MaterialInfo::CREATURE_BASE+j, i);
|
2012-11-05 11:48:24 -07:00
|
|
|
//addMaterialEntry(selected_category, material, basename+" (" + p->material[j]->id + "): " + material.toString(), set_defaults);
|
|
|
|
addMaterialEntry(selected_category, material, material.toString(), set_defaults);
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
}
|
2012-11-05 11:48:24 -07:00
|
|
|
|
|
|
|
materials_column.sort();
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool viewscreenChooseMaterial::addMaterialEntry(df::dfhack_material_category &selected_category, MaterialInfo &material,
|
|
|
|
string name, const bool set_defaults)
|
|
|
|
{
|
|
|
|
bool selected = false;
|
|
|
|
if (!selected_category.whole || material.matches(selected_category))
|
|
|
|
{
|
|
|
|
ListEntry<MaterialInfo> entry(pad_string(name, MAX_MATERIAL, false), material);
|
|
|
|
if (set_defaults)
|
|
|
|
{
|
|
|
|
if (cv->material.matches(material))
|
|
|
|
{
|
|
|
|
entry.selected = true;
|
|
|
|
selected = true;
|
2012-11-05 11:48:24 -07:00
|
|
|
/*if (materials_column.highlighted_index == 0)
|
|
|
|
materials_column.highlighted_index = materials_column.getBaseListSize();*/
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
materials_column.add(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return selected;
|
|
|
|
}
|
|
|
|
|
|
|
|
void viewscreenChooseMaterial::feed(set<df::interface_key> *input)
|
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
bool key_processed;
|
|
|
|
switch (selected_column)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
key_processed = items_column.feed(input);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
key_processed = masks_column.feed(input);
|
|
|
|
if (input->count(interface_key::SELECT))
|
|
|
|
populateMaterials(false);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
key_processed = materials_column.feed(input);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key_processed)
|
|
|
|
return;
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
if (input->count(interface_key::LEAVESCREEN))
|
|
|
|
{
|
|
|
|
input->clear();
|
|
|
|
Screen::dismiss(this);
|
|
|
|
return;
|
|
|
|
}
|
2012-11-05 11:48:24 -07:00
|
|
|
else if (input->count(interface_key::SEC_SELECT))
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
df::dfhack_material_category mat_mask;
|
|
|
|
vector<df::dfhack_material_category *> selected_masks = masks_column.getSelectedElems();
|
|
|
|
for (vector<df::dfhack_material_category *>::iterator it = selected_masks.begin(); it != selected_masks.end(); it++)
|
|
|
|
{
|
|
|
|
mat_mask.whole |= (*it)->whole;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
MaterialInfo *selected_material = materials_column.getFirstSelectedElem();
|
|
|
|
MaterialInfo material = (selected_material) ? *selected_material : MaterialInfo();
|
|
|
|
|
2012-11-10 07:00:55 -07:00
|
|
|
if (cv != NULL)
|
2012-11-05 11:48:24 -07:00
|
|
|
{
|
2012-11-10 07:00:55 -07:00
|
|
|
delete_constraint(cv);
|
2012-11-05 11:48:24 -07:00
|
|
|
}
|
2012-11-10 07:00:55 -07:00
|
|
|
|
|
|
|
ItemConstraint *nct = create_new_constraint(false, *items_column.getFirstSelectedElem(), material, mat_mask, item_quality::Ordinary, NULL, "");
|
|
|
|
nct->setGoalByCount(false);
|
|
|
|
nct->setGoalCount(10);
|
|
|
|
nct->setGoalGap(1);
|
|
|
|
|
|
|
|
/*else
|
2012-11-05 11:48:24 -07:00
|
|
|
{
|
|
|
|
cv->mat_mask = mat_mask;
|
|
|
|
cv->material = (selected_material) ? *selected_material : MaterialInfo();
|
2012-11-10 07:00:55 -07:00
|
|
|
}*/
|
2012-11-05 11:48:24 -07:00
|
|
|
|
|
|
|
reset_list = true;
|
|
|
|
Screen::dismiss(this);
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
else if (input->count(interface_key::CURSOR_LEFT))
|
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
--selected_column;
|
2013-01-02 02:27:26 -07:00
|
|
|
validateColumn();
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
else if (input->count(interface_key::CURSOR_RIGHT))
|
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
selected_column++;
|
2013-01-02 02:27:26 -07:00
|
|
|
validateColumn();
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
2012-11-05 11:48:24 -07:00
|
|
|
else if (enabler->tracking_on && enabler->mouse_lbut)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
if (items_column.setHighlightByMouse())
|
|
|
|
selected_column = 0;
|
|
|
|
else if (masks_column.setHighlightByMouse())
|
|
|
|
selected_column = 1;
|
|
|
|
else if (materials_column.setHighlightByMouse())
|
|
|
|
selected_column = 2;
|
|
|
|
|
|
|
|
enabler->mouse_lbut = enabler->mouse_rbut = 0;
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void viewscreenChooseMaterial::render()
|
|
|
|
{
|
|
|
|
if (Screen::isDismissed(this))
|
|
|
|
return;
|
|
|
|
|
|
|
|
dfhack_viewscreen::render();
|
|
|
|
|
|
|
|
Screen::clear();
|
|
|
|
Screen::drawBorder(" Workflow Material ");
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
items_column.display(selected_column == 0);
|
|
|
|
masks_column.display(selected_column == 1);
|
|
|
|
materials_column.display(selected_column == 2);
|
2012-10-24 05:09:34 -06:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
int32_t y = gps->dimy - 3;
|
|
|
|
int32_t x = 2;
|
|
|
|
OutputHotkeyString(x, y, "Save", "Shift-Enter");
|
|
|
|
x += 3;
|
|
|
|
OutputHotkeyString(x, y, "Cancel", "Esc");
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
void viewscreenChooseMaterial::validateColumn()
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
|
|
|
set_to_limit(selected_column, 2);
|
|
|
|
}
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
void viewscreenChooseMaterial::resize(int32_t x, int32_t y)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
dfhack_viewscreen::resize(x, y);
|
|
|
|
items_column.resize();
|
|
|
|
masks_column.resize();
|
|
|
|
materials_column.resize();
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
/*
|
|
|
|
* Inventory Monitor
|
|
|
|
*/
|
|
|
|
class viewscreenInventoryMonitor : public dfhack_viewscreen, public AdjustmentScreen
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
struct TableRow
|
|
|
|
{
|
|
|
|
vector< pair<int32_t, int32_t> > history_plot;
|
|
|
|
int32_t limit_y, gap_y;
|
|
|
|
ItemConstraint *cv;
|
2012-11-05 11:48:24 -07:00
|
|
|
UIColor value_color;
|
|
|
|
UIColor limit_color;
|
|
|
|
int severity;
|
2012-10-24 05:09:34 -06:00
|
|
|
};
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
class MonitorListColumn : public ListColumn<TableRow>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool order_by_severity;
|
2012-10-24 05:09:34 -06:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
virtual void sort()
|
|
|
|
{
|
|
|
|
if (!order_by_severity)
|
|
|
|
ListColumn::sort();
|
|
|
|
else
|
|
|
|
doSort(&compareSeverity);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void display_extras(const TableRow &row, int32_t &x, int32_t &y) const
|
|
|
|
{
|
|
|
|
string text;
|
|
|
|
text.append(int_to_string((row.cv->goalByCount()) ? row.cv->item_count : row.cv->item_amount));
|
|
|
|
text = pad_string(text, 6);
|
|
|
|
OutputString(row.value_color, x, y, text);
|
2012-10-24 05:09:34 -06:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
text = (row.cv->goalByCount()) ? " S " : " I ";
|
|
|
|
OutputString(COLOR_GREY, x, y, text);
|
|
|
|
|
|
|
|
text = int_to_string(row.cv->goalCount());
|
|
|
|
OutputString(row.limit_color, x, y, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool compareSeverity(ListEntry<TableRow> const& a, ListEntry<TableRow> const& b)
|
|
|
|
{
|
|
|
|
return a.elem.severity > b.elem.severity;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2012-10-24 05:09:34 -06:00
|
|
|
viewscreenInventoryMonitor();
|
|
|
|
|
|
|
|
void feed(set<df::interface_key> *input);
|
|
|
|
|
|
|
|
void render();
|
|
|
|
|
|
|
|
std::string getFocusString() { return "invmonitor"; }
|
|
|
|
|
|
|
|
virtual void resize(int32_t x, int32_t y)
|
|
|
|
{
|
|
|
|
dfhack_viewscreen::resize(x, y);
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2012-11-05 11:48:24 -07:00
|
|
|
MonitorListColumn rows;
|
|
|
|
int32_t bottom_controls_row;
|
2012-10-24 05:09:34 -06:00
|
|
|
int32_t divider_x;
|
|
|
|
int chart_width, chart_height;
|
|
|
|
int32_t axis_y_end, axis_y_start, axis_x_start, axis_x_end;
|
|
|
|
|
|
|
|
void init();
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
virtual void onConstraintChanged();
|
2012-10-24 05:09:34 -06:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
virtual void onModeChanged();
|
2012-10-24 05:09:34 -06:00
|
|
|
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
};
|
2012-10-24 05:09:34 -06:00
|
|
|
|
|
|
|
viewscreenInventoryMonitor::viewscreenInventoryMonitor()
|
|
|
|
{
|
|
|
|
adjustment_ui_display_start = 2;
|
|
|
|
chart_width = SIDEBAR_WIDTH - 2;
|
2012-11-05 11:48:24 -07:00
|
|
|
rows.order_by_severity = false;
|
|
|
|
|
|
|
|
rows.multiselect = false;
|
|
|
|
rows.allow_null = false;
|
|
|
|
rows.auto_select = true;
|
|
|
|
rows.title = pad_string("Item", MAX_ITEM_NAME, false);
|
|
|
|
rows.title += " ";
|
|
|
|
rows.title += pad_string("Material", MAX_MATERIAL, false);
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void viewscreenInventoryMonitor::init()
|
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
bottom_controls_row = gps->dimy - 3;
|
2012-10-24 05:09:34 -06:00
|
|
|
|
|
|
|
divider_x = gps->dimx - SIDEBAR_WIDTH - 2;
|
|
|
|
chart_height = min(SIDEBAR_WIDTH, gps->dimy - 20);
|
|
|
|
axis_y_end = gps->dimy - 3;
|
|
|
|
axis_y_start = axis_y_end - chart_height;
|
|
|
|
axis_x_start = divider_x + 2;
|
|
|
|
axis_x_end = axis_x_start + chart_width - 1;
|
|
|
|
|
|
|
|
rows.clear();
|
|
|
|
for (vector<ItemConstraint *>::iterator it = constraints.begin(); it < constraints.end(); it++)
|
|
|
|
{
|
|
|
|
TableRow row;
|
|
|
|
row.cv = *it;
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
if (row.cv->jobs.size() > 0)
|
|
|
|
{
|
|
|
|
ProtectedJob *pj = row.cv->jobs[0];
|
|
|
|
if (!pj->isActuallyResumed() && pj->want_resumed)
|
|
|
|
row.limit_color = COLOR_YELLOW;
|
|
|
|
else
|
|
|
|
row.limit_color = COLOR_GREY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
row.limit_color = COLOR_RED;
|
|
|
|
|
|
|
|
|
|
|
|
int curr_value = (row.cv->goalByCount()) ? row.cv->item_count : row.cv->item_amount;
|
|
|
|
row.value_color = COLOR_GREY;
|
|
|
|
if (curr_value >= row.cv->goalCount() - row.cv->goalGap())
|
|
|
|
row.value_color = COLOR_LIGHTGREEN;
|
|
|
|
else if (curr_value >= (row.cv->goalCount() - row.cv->goalGap() * 2))
|
|
|
|
row.value_color = COLOR_GREEN;
|
|
|
|
else if (curr_value <= row.cv->goalGap())
|
|
|
|
row.value_color = COLOR_LIGHTRED;
|
|
|
|
else if (curr_value <= (row.cv->goalGap() * 2))
|
|
|
|
row.value_color = COLOR_RED;
|
|
|
|
|
|
|
|
int max_val = row.cv->goalCount();
|
|
|
|
float scale_y;
|
|
|
|
if (max_history_days > 0 && row.cv->history.size() > 0)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
|
|
|
row.history_plot.clear();
|
2012-11-05 11:48:24 -07:00
|
|
|
max_val = max(max_val, *max_element(row.cv->history.begin(), row.cv->history.end()));
|
|
|
|
scale_y = (float) chart_height / (float) max_val;
|
2012-10-24 05:09:34 -06:00
|
|
|
float scale_x = (float) chart_width / (float) (max_history_days * 2);
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
int sumX, sumY, sumXY, sumXX;
|
|
|
|
sumX = sumY = sumXY = sumXX = 0;
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
for (size_t i = 0; i < row.cv->history.size(); i++)
|
|
|
|
{
|
|
|
|
pair<int32_t, int32_t> point(axis_x_start + (int) (scale_x * (float) i),
|
|
|
|
axis_y_end - (int) (scale_y * (float) row.cv->history[i]));
|
|
|
|
|
|
|
|
row.history_plot.push_back(point);
|
2012-11-05 11:48:24 -07:00
|
|
|
|
|
|
|
if (row.value_color == COLOR_GREY)
|
|
|
|
{
|
|
|
|
sumX += i;
|
|
|
|
sumY += row.cv->history[i];
|
|
|
|
sumXY += i * row.cv->history[i];
|
|
|
|
sumXX += i*i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (row.value_color == COLOR_GREY)
|
|
|
|
{
|
|
|
|
int count = row.cv->history.size();
|
|
|
|
float slope = (float) (count * sumXY - sumX * sumY) /
|
|
|
|
(float) (count * sumXX - sumX * sumX);
|
|
|
|
|
|
|
|
if (slope > 0.3f)
|
|
|
|
row.value_color = COLOR_GREEN;
|
|
|
|
else if (slope < -0.3f)
|
|
|
|
row.value_color = COLOR_RED;
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
2012-11-05 11:48:24 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
scale_y = (float) chart_height / (float) max_val;
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
row.limit_y = axis_y_end - (int) (scale_y * (float) row.cv->goalCount());
|
|
|
|
row.gap_y = axis_y_end - (int) (scale_y * (float) (row.cv->goalCount() - row.cv->goalGap()));
|
2012-10-24 05:09:34 -06:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
string text = pad_string(row.cv->item.toString(), MAX_ITEM_NAME, false);
|
|
|
|
text += " ";
|
|
|
|
text += pad_string(get_constraint_material(row.cv), MAX_MATERIAL, false, true);
|
|
|
|
|
|
|
|
switch (row.value_color)
|
|
|
|
{
|
|
|
|
case COLOR_LIGHTGREEN:
|
|
|
|
row.severity = 0;
|
|
|
|
break;
|
|
|
|
case COLOR_GREEN:
|
|
|
|
row.severity = 1;
|
|
|
|
break;
|
|
|
|
case COLOR_GREY:
|
|
|
|
row.severity = 2;
|
|
|
|
break;
|
|
|
|
case COLOR_RED:
|
|
|
|
row.severity = 3;
|
|
|
|
break;
|
|
|
|
case COLOR_LIGHTRED:
|
|
|
|
row.severity = 4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rows.add(text, row);
|
|
|
|
}
|
|
|
|
rows.sort();
|
|
|
|
rows.changeHighlight(0);
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void viewscreenInventoryMonitor::onConstraintChanged()
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
void viewscreenInventoryMonitor::onModeChanged()
|
|
|
|
{
|
|
|
|
TableRow *row = rows.getFirstSelectedElem();
|
|
|
|
if (!row)
|
|
|
|
return;
|
|
|
|
row->cv->history.clear();
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
void viewscreenInventoryMonitor::render()
|
|
|
|
{
|
|
|
|
if (Screen::isDismissed(this))
|
|
|
|
return;
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
if (viewscreenChooseMaterial::reset_list)
|
|
|
|
{
|
|
|
|
viewscreenChooseMaterial::reset_list = false;
|
|
|
|
init();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
dfhack_viewscreen::render();
|
|
|
|
|
|
|
|
Screen::clear();
|
|
|
|
Screen::drawBorder(" Inventory Monitor ");
|
|
|
|
|
|
|
|
Screen::Pen border('\xDB', 8);
|
|
|
|
for (int32_t y = 1; y < gps->dimy - 1; y++)
|
|
|
|
{
|
|
|
|
paintTile(border, divider_x, y);
|
|
|
|
}
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
int32_t x = MAX_ITEM_NAME + MAX_MATERIAL + 3;
|
|
|
|
int32_t y = 2;
|
|
|
|
paint_text(COLOR_TITLE, x, y, "Stock / Limit");
|
2012-10-24 05:09:34 -06:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
rows.display(true);
|
2012-10-24 05:09:34 -06:00
|
|
|
|
|
|
|
y = bottom_controls_row;
|
|
|
|
x = 2;
|
2012-11-05 11:48:24 -07:00
|
|
|
OutputHotkeyString(x, y, "Add", "a");
|
|
|
|
x += 2;
|
2012-10-24 05:09:34 -06:00
|
|
|
OutputHotkeyString(x, y, "Edit", "e");
|
2012-11-05 11:48:24 -07:00
|
|
|
|
|
|
|
x += 2;
|
|
|
|
OutputHotkeyString(x, y, (rows.order_by_severity) ? "Name Order" : "Severity Order", "o");
|
2012-10-24 05:09:34 -06:00
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
if (rows.getDisplayListSize() > 0)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
TableRow *row = rows.getFirstSelectedElem();
|
2012-11-29 03:15:24 -07:00
|
|
|
AdjustmentScreen::render(row->cv, true);
|
2012-10-24 05:09:34 -06:00
|
|
|
|
|
|
|
if (max_history_days > 0)
|
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
paint_text(COLOR_BROWN, axis_x_start, axis_y_start-2, " Stock History Chart");
|
2012-10-24 05:09:34 -06:00
|
|
|
Screen::Pen y_axis('\xB3', COLOR_BROWN);
|
|
|
|
x = axis_x_start;
|
|
|
|
for (y = axis_y_start; y <= axis_y_end; y++)
|
|
|
|
{
|
|
|
|
paintTile(y_axis, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
Screen::Pen up_arrow('\xCF', COLOR_BROWN);
|
|
|
|
paintTile(up_arrow, axis_x_start, axis_y_start-1);
|
|
|
|
|
|
|
|
Screen::Pen x_axis('\xC4', COLOR_BROWN);
|
|
|
|
y = axis_y_end;
|
|
|
|
for (x = axis_x_start; x <= axis_x_end; x++)
|
|
|
|
{
|
|
|
|
paintTile(x_axis, x, y);
|
2012-11-05 11:48:24 -07:00
|
|
|
paint_text(COLOR_LIGHTGREEN, x, row->limit_y, "-");
|
|
|
|
paint_text(COLOR_GREEN, x, row->gap_y, "-");
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Screen::Pen right_arrow('\xAF', COLOR_BROWN);
|
|
|
|
paintTile(right_arrow, axis_x_end+1, axis_y_end);
|
|
|
|
|
|
|
|
Screen::Pen zero_axis('\x9E', COLOR_BROWN);
|
|
|
|
paintTile(zero_axis, axis_x_start, axis_y_end);
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
for (size_t i = 0; i < row->history_plot.size(); i++)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
int x = row->history_plot[i].first;
|
|
|
|
int y = row->history_plot[i].second;
|
2012-10-24 05:09:34 -06:00
|
|
|
paint_text(COLOR_CYAN, x, y, "*");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2012-11-05 11:48:24 -07:00
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
void viewscreenInventoryMonitor::feed(set<df::interface_key> *input)
|
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
if (!rows.search_entry_mode && rows.getDisplayListSize() > 0 && AdjustmentScreen::feed(input, rows.getFirstSelectedElem()->cv))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (rows.feed(input))
|
2012-10-24 05:09:34 -06:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (input->count(interface_key::LEAVESCREEN))
|
|
|
|
{
|
|
|
|
input->clear();
|
|
|
|
Screen::dismiss(this);
|
|
|
|
return;
|
|
|
|
}
|
2012-11-05 11:48:24 -07:00
|
|
|
else if (input->count(interface_key::CUSTOM_O))
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
rows.order_by_severity = !rows.order_by_severity;
|
|
|
|
rows.sort();
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
2012-11-05 11:48:24 -07:00
|
|
|
else if ((input->count(interface_key::CUSTOM_E) || input->count(interface_key::SELECT)) && rows.getDisplayListSize() > 0)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
TableRow *row = rows.getFirstSelectedElem();
|
|
|
|
if (row)
|
|
|
|
{
|
|
|
|
Screen::show(new viewscreenChooseMaterial(row->cv));
|
|
|
|
}
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
2012-11-05 11:48:24 -07:00
|
|
|
else if (input->count(interface_key::CUSTOM_A))
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
Screen::show(new viewscreenChooseMaterial());
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
2012-11-05 11:48:24 -07:00
|
|
|
else if (enabler->tracking_on)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
2012-11-05 11:48:24 -07:00
|
|
|
enabler->mouse_lbut = enabler->mouse_rbut = 0;
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************
|
|
|
|
* Hook for workshop view *
|
|
|
|
******************************/
|
|
|
|
struct wf_workshop_hook : public df::viewscreen_dwarfmodest
|
|
|
|
{
|
|
|
|
typedef df::viewscreen_dwarfmodest interpose_base;
|
|
|
|
|
|
|
|
static color_ostream_proxy console_out;
|
|
|
|
static df::job *last_job;
|
|
|
|
static df::job *job;
|
|
|
|
static AdjustmentScreen dialog;
|
|
|
|
|
|
|
|
bool checkJobSelection()
|
|
|
|
{
|
|
|
|
if (!enabled)
|
2012-11-05 11:48:24 -07:00
|
|
|
return false;
|
2012-10-24 05:09:34 -06:00
|
|
|
|
|
|
|
if (!first_update_done)
|
|
|
|
{
|
|
|
|
plugin_onupdate(console_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
job = Gui::getSelectedWorkshopJob(console_out, true);
|
|
|
|
if (job != last_job)
|
|
|
|
{
|
|
|
|
dialog.reset();
|
|
|
|
last_job = job;
|
|
|
|
}
|
|
|
|
|
|
|
|
return job != NULL;
|
|
|
|
}
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
bool can_enable_plugin()
|
|
|
|
{
|
|
|
|
return (!enabled &&
|
|
|
|
Gui::dwarfmode_hotkey(Core::getTopViewscreen()) &&
|
|
|
|
ui->main.mode == ui_sidebar_mode::QueryBuilding &&
|
|
|
|
Gui::getSelectedWorkshopJob(console_out, true));
|
|
|
|
}
|
|
|
|
|
2012-11-09 07:47:15 -07:00
|
|
|
ProtectedJob *get_protected_job(int32_t id)
|
|
|
|
{
|
|
|
|
ProtectedJob *pj = get_known(id);
|
|
|
|
if (!pj && job->flags.bits.repeat)
|
|
|
|
{
|
|
|
|
pj = add_known_job(job);
|
|
|
|
compute_job_outputs(console_out, pj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pj;
|
|
|
|
}
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
bool handleInput(set<df::interface_key> *input)
|
|
|
|
{
|
|
|
|
bool key_processed = true;
|
|
|
|
if (checkJobSelection())
|
|
|
|
{
|
2012-11-09 07:47:15 -07:00
|
|
|
ProtectedJob *pj = get_protected_job(job->id);
|
2012-10-24 05:09:34 -06:00
|
|
|
ItemConstraint *cv = NULL;
|
2012-11-09 07:47:15 -07:00
|
|
|
if (pj && pj->constraints.size() > 0)
|
2012-10-24 05:09:34 -06:00
|
|
|
cv = pj->constraints[0];
|
|
|
|
|
|
|
|
if (!dialog.feed(input, cv, pj))
|
|
|
|
{
|
|
|
|
if (input->count(interface_key::CUSTOM_T))
|
|
|
|
{
|
|
|
|
if (!cv)
|
|
|
|
{
|
|
|
|
// Add tracking
|
2012-11-09 07:47:15 -07:00
|
|
|
if (!pj)
|
|
|
|
pj = add_known_job(job);
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
compute_job_outputs(console_out, pj, true);
|
2012-11-05 11:48:24 -07:00
|
|
|
if (pj->constraints.size() > 0)
|
|
|
|
{
|
|
|
|
cv = pj->constraints[0];
|
2012-11-09 07:47:15 -07:00
|
|
|
if (cv->goalCount() == -1)
|
|
|
|
{
|
|
|
|
cv->setGoalByCount(false);
|
|
|
|
cv->setGoalCount(10);
|
|
|
|
cv->setGoalGap(1);
|
|
|
|
}
|
2012-11-05 11:48:24 -07:00
|
|
|
job->flags.bits.repeat = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Gui::showAnnouncement("Job type not currently supported", 6, true);
|
|
|
|
}
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
}
|
2012-11-05 11:48:24 -07:00
|
|
|
else if (input->count(interface_key::CUSTOM_M))
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
|
|
|
Screen::show(new viewscreenInventoryMonitor());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
key_processed = false;
|
|
|
|
}
|
|
|
|
}
|
2012-11-05 11:48:24 -07:00
|
|
|
else if (can_enable_plugin() && input->count(interface_key::CUSTOM_W))
|
|
|
|
{
|
|
|
|
enable_plugin(console_out);
|
|
|
|
plugin_onupdate(console_out);
|
|
|
|
}
|
2012-10-24 05:09:34 -06:00
|
|
|
else
|
|
|
|
key_processed = false;
|
|
|
|
|
|
|
|
return key_processed;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_VMETHOD_INTERPOSE(void, feed, (set<df::interface_key> *input))
|
|
|
|
{
|
|
|
|
if (!handleInput(input))
|
|
|
|
INTERPOSE_NEXT(feed)(input);
|
|
|
|
else
|
|
|
|
input->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_VMETHOD_INTERPOSE(void, render, ())
|
|
|
|
{
|
|
|
|
INTERPOSE_NEXT(render)();
|
|
|
|
if (checkJobSelection())
|
|
|
|
{
|
|
|
|
ItemConstraint *cv = NULL;
|
2012-11-09 07:47:15 -07:00
|
|
|
ProtectedJob *pj = get_protected_job(job->id);
|
|
|
|
if (pj && pj->constraints.size() > 0)
|
2012-10-24 05:09:34 -06:00
|
|
|
{
|
|
|
|
cv = pj->constraints[0];
|
|
|
|
}
|
2012-11-09 07:47:15 -07:00
|
|
|
|
2012-11-29 03:15:24 -07:00
|
|
|
dialog.render(cv, false);
|
2012-11-05 11:48:24 -07:00
|
|
|
if (cv)
|
|
|
|
++dialog.y;
|
|
|
|
OutputHotkeyString(dialog.left_margin, dialog.y, "Inventory Monitor", "m");
|
|
|
|
}
|
|
|
|
else if (can_enable_plugin())
|
|
|
|
{
|
2012-11-29 03:15:24 -07:00
|
|
|
int x = get_left_margin();
|
2012-11-05 11:48:24 -07:00
|
|
|
int y = 24;
|
|
|
|
OutputHotkeyString(x, y, "Enable Workflow", "w");
|
2012-10-24 05:09:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
color_ostream_proxy wf_workshop_hook::console_out(Core::getInstance().getConsole());
|
|
|
|
df::job *wf_workshop_hook::job = NULL;
|
|
|
|
df::job *wf_workshop_hook::last_job = NULL;
|
|
|
|
AdjustmentScreen wf_workshop_hook::dialog;
|
|
|
|
|
|
|
|
|
|
|
|
IMPLEMENT_VMETHOD_INTERPOSE(wf_workshop_hook, feed);
|
|
|
|
IMPLEMENT_VMETHOD_INTERPOSE(wf_workshop_hook, render);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef INV_MONITOR_COL_COUNT
|
|
|
|
#undef MAX_ITEM_NAME
|
|
|
|
|
2012-11-05 11:48:24 -07:00
|
|
|
|
|
|
|
static command_result workflow_cmd(color_ostream &out, vector <string> & parameters)
|
|
|
|
{
|
|
|
|
CoreSuspender suspend;
|
|
|
|
|
|
|
|
if (!Core::getInstance().isWorldLoaded()) {
|
|
|
|
out.printerr("World is not loaded: please load a game first.\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enabled) {
|
|
|
|
check_lost_jobs(out, 0);
|
|
|
|
recover_jobs(out);
|
|
|
|
update_job_data(out);
|
|
|
|
map_job_constraints(out);
|
|
|
|
map_job_items(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
df::building *workshop = NULL;
|
|
|
|
//FIXME: unused variable!
|
|
|
|
//df::job *job = NULL;
|
|
|
|
|
|
|
|
if (Gui::dwarfmode_hotkey(Core::getTopViewscreen()) &&
|
|
|
|
ui->main.mode == ui_sidebar_mode::QueryBuilding)
|
|
|
|
{
|
|
|
|
workshop = world->selected_building;
|
|
|
|
//job = Gui::getSelectedWorkshopJob(out, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string cmd = parameters.empty() ? "list" : parameters[0];
|
|
|
|
|
|
|
|
if (cmd == "enable" || cmd == "disable")
|
|
|
|
{
|
|
|
|
bool enable = (cmd == "enable");
|
|
|
|
if (enable && !enabled)
|
|
|
|
{
|
|
|
|
enable_plugin(out);
|
|
|
|
}
|
|
|
|
else if (!enable && parameters.size() == 1)
|
|
|
|
{
|
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
enabled = false;
|
|
|
|
setOptionEnabled(CF_ENABLED, false);
|
|
|
|
stop_protect(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
out << "The plugin is disabled." << endl;
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 1; i < parameters.size(); i++)
|
|
|
|
{
|
|
|
|
if (parameters[i] == "drybuckets")
|
|
|
|
setOptionEnabled(CF_DRYBUCKETS, enable);
|
|
|
|
else if (parameters[i] == "auto-melt")
|
|
|
|
setOptionEnabled(CF_AUTOMELT, enable);
|
|
|
|
else
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enabled)
|
|
|
|
out << "The plugin is enabled." << endl;
|
|
|
|
else
|
|
|
|
out << "The plugin is disabled." << endl;
|
|
|
|
|
|
|
|
if (isOptionEnabled(CF_DRYBUCKETS))
|
|
|
|
out << "Option drybuckets is enabled." << endl;
|
|
|
|
if (isOptionEnabled(CF_AUTOMELT))
|
|
|
|
out << "Option auto-melt is enabled." << endl;
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
else if (cmd == "count" || cmd == "amount")
|
|
|
|
{
|
|
|
|
if (!enabled)
|
|
|
|
enable_plugin(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!enabled)
|
|
|
|
out << "Note: the plugin is not enabled." << endl;
|
|
|
|
|
|
|
|
if (cmd == "jobs")
|
|
|
|
{
|
|
|
|
if (workshop)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < workshop->jobs.size(); i++)
|
|
|
|
print_job(out, get_known(workshop->jobs[i]->id));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (TKnownJobs::iterator it = known_jobs.begin(); it != known_jobs.end(); ++it)
|
|
|
|
if (it->second->isLive())
|
|
|
|
print_job(out, it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pending = false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < pending_recover.size(); i++)
|
|
|
|
{
|
|
|
|
if (!workshop || pending_recover[i]->holder == workshop)
|
|
|
|
{
|
|
|
|
if (!pending)
|
|
|
|
{
|
|
|
|
out.print("\nPending recovery:\n");
|
|
|
|
pending = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Job::printJobDetails(out, pending_recover[i]->job_copy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
else if (cmd == "list")
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < constraints.size(); i++)
|
|
|
|
print_constraint(out, constraints[i]);
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
else if (cmd == "list-commands")
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < constraints.size(); i++)
|
|
|
|
{
|
|
|
|
auto cv = constraints[i];
|
|
|
|
out << "workflow " << (cv->goalByCount() ? "count " : "amount ")
|
|
|
|
<< cv->config.val() << " " << cv->goalCount() << " " << cv->goalGap() << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
else if (cmd == "count" || cmd == "amount")
|
|
|
|
{
|
|
|
|
if (parameters.size() < 3)
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
|
|
|
int limit = atoi(parameters[2].c_str());
|
|
|
|
if (limit <= 0) {
|
|
|
|
out.printerr("Invalid limit value.\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemConstraint *icv = get_constraint(out, parameters[1]);
|
|
|
|
if (!icv)
|
|
|
|
return CR_FAILURE;
|
|
|
|
|
|
|
|
icv->setGoalByCount(cmd == "count");
|
|
|
|
icv->setGoalCount(limit);
|
|
|
|
if (parameters.size() >= 4)
|
|
|
|
icv->setGoalGap(atoi(parameters[3].c_str()));
|
|
|
|
else
|
|
|
|
icv->setGoalGap(-1);
|
|
|
|
|
|
|
|
process_constraints(out);
|
|
|
|
print_constraint(out, icv);
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
else if (cmd == "unlimit")
|
|
|
|
{
|
|
|
|
if (parameters.size() != 2)
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < constraints.size(); i++)
|
|
|
|
{
|
|
|
|
if (constraints[i]->config.val() != parameters[1])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
delete_constraint(constraints[i]);
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
out.printerr("Constraint not found: %s\n", parameters[1].c_str());
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
else if (cmd == "unlimit-all")
|
|
|
|
{
|
|
|
|
if (parameters.size() != 1)
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
|
|
|
while (!constraints.empty())
|
|
|
|
delete_constraint(constraints[0]);
|
|
|
|
|
|
|
|
out.print("Removed all constraints.\n");
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
else if (cmd == "monitor")
|
|
|
|
{
|
|
|
|
if (!enabled)
|
|
|
|
{
|
|
|
|
enable_plugin(out);
|
|
|
|
plugin_onupdate(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
Screen::show(new wf_ui::viewscreenInventoryMonitor());
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
|
2012-10-24 05:09:34 -06:00
|
|
|
DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands)
|
|
|
|
{
|
|
|
|
if (!world || !ui)
|
|
|
|
return CR_FAILURE;
|
|
|
|
|
|
|
|
if (!gps || !INTERPOSE_HOOK(wf_ui::wf_workshop_hook, feed).apply() || !INTERPOSE_HOOK(wf_ui::wf_workshop_hook, render).apply())
|
|
|
|
out.printerr("Could not insert Workflow hooks!\n");
|
|
|
|
|
|
|
|
if (ui_workshop_job_cursor && job_next_id) {
|
|
|
|
commands.push_back(
|
|
|
|
PluginCommand(
|
|
|
|
"workflow", "Manage control of repeat jobs.",
|
|
|
|
workflow_cmd, false,
|
|
|
|
" workflow enable [option...]\n"
|
|
|
|
" workflow disable [option...]\n"
|
|
|
|
" If no options are specified, enables or disables the plugin.\n"
|
|
|
|
" Otherwise, enables or disables any of the following options:\n"
|
|
|
|
" - drybuckets: Automatically empty abandoned water buckets.\n"
|
|
|
|
" - auto-melt: Resume melt jobs when there are objects to melt.\n"
|
|
|
|
" workflow jobs\n"
|
|
|
|
" List workflow-controlled jobs (if in a workshop, filtered by it).\n"
|
|
|
|
" workflow list\n"
|
|
|
|
" List active constraints, and their job counts.\n"
|
|
|
|
" workflow list-commands\n"
|
|
|
|
" List workflow commands that re-create existing constraints.\n"
|
|
|
|
" workflow count <constraint-spec> <cnt-limit> [cnt-gap]\n"
|
|
|
|
" workflow amount <constraint-spec> <cnt-limit> [cnt-gap]\n"
|
|
|
|
" Set a constraint. The first form counts each stack as only 1 item.\n"
|
|
|
|
" workflow unlimit <constraint-spec>\n"
|
|
|
|
" Delete a constraint.\n"
|
|
|
|
" workflow unlimit-all\n"
|
|
|
|
" Delete all constraints.\n"
|
|
|
|
"Function:\n"
|
|
|
|
" - When the plugin is enabled, it protects all repeat jobs from removal.\n"
|
|
|
|
" If they do disappear due to any cause, they are immediately re-added\n"
|
|
|
|
" to their workshop and suspended.\n"
|
|
|
|
" - In addition, when any constraints on item amounts are set, repeat jobs\n"
|
|
|
|
" that produce that kind of item are automatically suspended and resumed\n"
|
|
|
|
" as the item amount goes above or below the limit. The gap specifies how\n"
|
|
|
|
" much below the limit the amount has to drop before jobs are resumed;\n"
|
|
|
|
" this is intended to reduce the frequency of jobs being toggled.\n"
|
|
|
|
"Constraint examples:\n"
|
|
|
|
" workflow amount AMMO:ITEM_AMMO_BOLTS/METAL 1000 100\n"
|
|
|
|
" workflow amount AMMO:ITEM_AMMO_BOLTS/WOOD,BONE 200 50\n"
|
|
|
|
" Keep metal bolts within 900-1000, and wood/bone within 150-200.\n"
|
|
|
|
" workflow count FOOD 120 30\n"
|
|
|
|
" workflow count DRINK 120 30\n"
|
|
|
|
" Keep the number of prepared food & drink stacks between 90 and 120\n"
|
|
|
|
" workflow count BIN 30\n"
|
|
|
|
" workflow count BARREL 30\n"
|
|
|
|
" workflow count BOX/CLOTH,SILK,YARN 30\n"
|
|
|
|
" Make sure there are always 25-30 empty bins/barrels/bags.\n"
|
|
|
|
" workflow count BAR//COAL 20\n"
|
|
|
|
" workflow count BAR//COPPER 30\n"
|
|
|
|
" Make sure there are always 15-20 coal and 25-30 copper bars.\n"
|
|
|
|
" workflow count CRAFTS//GOLD 20\n"
|
|
|
|
" Produce 15-20 gold crafts.\n"
|
|
|
|
" workflow count POWDER_MISC/SAND 20\n"
|
|
|
|
" workflow count BOULDER/CLAY 20\n"
|
|
|
|
" Collect 15-20 sand bags and clay boulders.\n"
|
|
|
|
" workflow amount POWDER_MISC//MUSHROOM_CUP_DIMPLE:MILL 100 20\n"
|
|
|
|
" Make sure there are always 80-100 units of dimple dye.\n"
|
|
|
|
" In order for this to work, you have to set the material of\n"
|
|
|
|
" the PLANT input on the Mill Plants job to MUSHROOM_CUP_DIMPLE\n"
|
|
|
|
" using the 'job item-material' command.\n"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
init_state(out);
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_shutdown (color_ostream &out)
|
|
|
|
{
|
|
|
|
INTERPOSE_HOOK(wf_ui::wf_workshop_hook, feed).remove();
|
|
|
|
INTERPOSE_HOOK(wf_ui::wf_workshop_hook, render).remove();
|
|
|
|
|
|
|
|
cleanup_state(out);
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
|
|
|
|
{
|
|
|
|
switch (event) {
|
|
|
|
case SC_MAP_LOADED:
|
|
|
|
cleanup_state(out);
|
|
|
|
init_state(out);
|
|
|
|
break;
|
|
|
|
case SC_MAP_UNLOADED:
|
|
|
|
cleanup_state(out);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|