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-10-23 11:42:03 -06:00
|
|
|
#include "LuaTools.h"
|
|
|
|
#include "DataFuncs.h"
|
|
|
|
|
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-10-25 03:20:41 -06:00
|
|
|
#include "df/vehicle.h"
|
2012-01-07 10:47:23 -07:00
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
|
|
|
using std::endl;
|
2013-10-08 07:41:49 -06:00
|
|
|
using std::flush;
|
2012-01-07 10:47:23 -07:00
|
|
|
using namespace DFHack;
|
|
|
|
using namespace df::enums;
|
|
|
|
|
2014-12-06 16:47:35 -07:00
|
|
|
DFHACK_PLUGIN("workflow");
|
|
|
|
|
|
|
|
REQUIRE_GLOBAL(world);
|
|
|
|
REQUIRE_GLOBAL(ui);
|
|
|
|
REQUIRE_GLOBAL(ui_workshop_job_cursor);
|
|
|
|
REQUIRE_GLOBAL(job_next_id);
|
2012-01-07 10:47:23 -07:00
|
|
|
|
|
|
|
/* 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-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands)
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
|
|
|
if (!world || !ui)
|
|
|
|
return CR_FAILURE;
|
|
|
|
|
|
|
|
if (ui_workshop_job_cursor && job_next_id) {
|
|
|
|
commands.push_back(
|
|
|
|
PluginCommand(
|
2012-01-09 06:29:28 -07:00
|
|
|
"workflow", "Manage control of repeat jobs.",
|
|
|
|
workflow_cmd, false,
|
2012-01-12 09:07:53 -07:00
|
|
|
" 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"
|
2012-01-09 08:20:28 -07:00
|
|
|
" workflow jobs\n"
|
2012-01-09 06:29:28 -07:00
|
|
|
" List workflow-controlled jobs (if in a workshop, filtered by it).\n"
|
2012-01-09 08:20:28 -07:00
|
|
|
" workflow list\n"
|
|
|
|
" List active constraints, and their job counts.\n"
|
2012-03-14 02:09:02 -06:00
|
|
|
" workflow list-commands\n"
|
|
|
|
" List workflow commands that re-create existing constraints.\n"
|
2012-01-11 09:04:04 -07:00
|
|
|
" 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"
|
2012-01-09 08:20:28 -07:00
|
|
|
" workflow unlimit <constraint-spec>\n"
|
|
|
|
" Delete a constraint.\n"
|
2012-03-14 02:09:02 -06:00
|
|
|
" workflow unlimit-all\n"
|
|
|
|
" Delete all constraints.\n"
|
2012-01-09 06:29:28 -07:00
|
|
|
"Function:\n"
|
2012-01-09 10:25:55 -07:00
|
|
|
" - 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"
|
2012-01-10 06:23:37 -07:00
|
|
|
" 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"
|
2012-11-10 23:58:05 -07:00
|
|
|
"Constraint format:\n"
|
|
|
|
" The contstraint spec consists of 4 parts, separated with '/' characters:\n"
|
|
|
|
" ITEM[:SUBTYPE]/[GENERIC_MAT,...]/[SPECIFIC_MAT:...]/[LOCAL,<quality>]\n"
|
|
|
|
" The first part is mandatory and specifies the item type and subtype,\n"
|
|
|
|
" using the raw tokens for items, in the same syntax you would e.g. use\n"
|
|
|
|
" for a custom reaction input. The subsequent parts are optional:\n"
|
|
|
|
" - A generic material spec constrains the item material to one of\n"
|
|
|
|
" the hard-coded generic classes, like WOOD, METAL, YARN or MILK.\n"
|
|
|
|
" - A specific material spec chooses the material exactly, using the\n"
|
|
|
|
" raw syntax for reaction input materials, e.g. INORGANIC:IRON,\n"
|
|
|
|
" although for convenience it also allows just IRON, or ACACIA:WOOD.\n"
|
|
|
|
" - A comma-separated list of miscellaneous flags, which currently can\n"
|
|
|
|
" be used to ignore imported items or items below a certain quality.\n"
|
2012-01-09 10:25:55 -07:00
|
|
|
"Constraint examples:\n"
|
2012-01-11 09:04:04 -07:00
|
|
|
" workflow amount AMMO:ITEM_AMMO_BOLTS/METAL 1000 100\n"
|
|
|
|
" workflow amount AMMO:ITEM_AMMO_BOLTS/WOOD,BONE 200 50\n"
|
2012-01-10 06:23:37 -07:00
|
|
|
" Keep metal bolts within 900-1000, and wood/bone within 150-200.\n"
|
2012-01-11 09:04:04 -07:00
|
|
|
" workflow count FOOD 120 30\n"
|
|
|
|
" workflow count DRINK 120 30\n"
|
2012-01-10 06:23:37 -07:00
|
|
|
" Keep the number of prepared food & drink stacks between 90 and 120\n"
|
2012-01-11 09:04:04 -07:00
|
|
|
" workflow count BIN 30\n"
|
|
|
|
" workflow count BARREL 30\n"
|
|
|
|
" workflow count BOX/CLOTH,SILK,YARN 30\n"
|
2012-01-10 06:23:37 -07:00
|
|
|
" Make sure there are always 25-30 empty bins/barrels/bags.\n"
|
2012-01-11 09:04:04 -07:00
|
|
|
" workflow count BAR//COAL 20\n"
|
|
|
|
" workflow count BAR//COPPER 30\n"
|
2012-01-10 06:23:37 -07:00
|
|
|
" Make sure there are always 15-20 coal and 25-30 copper bars.\n"
|
2012-03-14 02:09:02 -06:00
|
|
|
" workflow count CRAFTS//GOLD 20\n"
|
|
|
|
" Produce 15-20 gold crafts.\n"
|
2012-01-11 09:04:04 -07:00
|
|
|
" workflow count POWDER_MISC/SAND 20\n"
|
|
|
|
" workflow count BOULDER/CLAY 20\n"
|
2012-01-10 06:23:37 -07:00
|
|
|
" Collect 15-20 sand bags and clay boulders.\n"
|
2012-01-11 09:04:04 -07:00
|
|
|
" workflow amount POWDER_MISC//MUSHROOM_CUP_DIMPLE:MILL 100 20\n"
|
2012-01-10 06:23:37 -07:00
|
|
|
" 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"
|
2012-11-10 23:58:05 -07:00
|
|
|
" workflow count CRAFTS///LOCAL,EXCEPTIONAL 100 90\n"
|
|
|
|
" Maintain 10-100 locally-made crafts of exceptional quality.\n"
|
2012-01-07 10:47:23 -07:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
init_state(out);
|
2012-01-07 10:47:23 -07:00
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_shutdown (color_ostream &out)
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
cleanup_state(out);
|
2012-01-07 10:47:23 -07:00
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
|
|
|
switch (event) {
|
2012-03-31 18:56:54 -06:00
|
|
|
case SC_MAP_LOADED:
|
2012-03-10 04:55:42 -07:00
|
|
|
cleanup_state(out);
|
|
|
|
init_state(out);
|
2012-01-07 10:47:23 -07:00
|
|
|
break;
|
2012-03-31 18:56:54 -06:00
|
|
|
case SC_MAP_UNLOADED:
|
2012-03-10 04:55:42 -07:00
|
|
|
cleanup_state(out);
|
2012-01-07 10:47:23 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
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-11-30 03:48:05 -07:00
|
|
|
static const size_t MAX_HISTORY_SIZE = 28;
|
|
|
|
|
|
|
|
enum HistoryItem {
|
|
|
|
HIST_COUNT = 0,
|
|
|
|
HIST_AMOUNT,
|
|
|
|
HIST_INUSE_COUNT,
|
|
|
|
HIST_INUSE_AMOUNT
|
|
|
|
};
|
|
|
|
|
2012-01-09 08:20:28 -07:00
|
|
|
struct ItemConstraint {
|
|
|
|
PersistentDataItem config;
|
2012-11-28 08:25:01 -07:00
|
|
|
PersistentDataItem history;
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-11-10 23:58:05 -07:00
|
|
|
// Fixed key parsed into fields
|
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
|
|
|
|
2012-11-10 23:58:05 -07:00
|
|
|
item_quality::item_quality min_quality;
|
|
|
|
bool is_local;
|
|
|
|
|
|
|
|
// Tracking data
|
2012-01-09 08:20:28 -07:00
|
|
|
int weight;
|
|
|
|
std::vector<ProtectedJob*> jobs;
|
|
|
|
|
2012-11-28 08:25:01 -07:00
|
|
|
int item_amount, item_count, item_inuse_amount, item_inuse_count;
|
2012-01-09 10:25:55 -07:00
|
|
|
bool request_suspend, request_resume;
|
|
|
|
|
2012-01-15 04:39:20 -07:00
|
|
|
bool is_active, cant_resume_reported;
|
2012-12-02 04:31:43 -07:00
|
|
|
int low_stock_reported;
|
2012-01-15 04:39:20 -07:00
|
|
|
|
2012-01-09 10:25:55 -07:00
|
|
|
TMaterialCache material_cache;
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
public:
|
2012-01-15 04:39:20 -07:00
|
|
|
ItemConstraint()
|
2012-11-10 23:58:05 -07:00
|
|
|
: is_craft(false), min_quality(item_quality::Ordinary), is_local(false),
|
2012-11-28 08:25:01 -07:00
|
|
|
weight(0), item_amount(0), item_count(0), item_inuse_amount(0), item_inuse_count(0),
|
2012-12-02 04:31:43 -07:00
|
|
|
is_active(false), cant_resume_reported(false), low_stock_reported(-1)
|
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() {
|
2012-11-19 07:59:58 -07:00
|
|
|
int cval = (config.ival(1) <= 0) ? std::min(5,goalCount()/2) : config.ival(1);
|
|
|
|
return std::max(1, std::min(goalCount()-1, cval));
|
2012-01-09 08:20:28 -07:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-12-02 04:31:43 -07:00
|
|
|
int curItemStock() { return goalByCount() ? item_count : item_amount; }
|
|
|
|
|
2012-01-09 10:25:55 -07:00
|
|
|
void init(const std::string &str)
|
|
|
|
{
|
|
|
|
config.val() = str;
|
2012-10-24 11:49:30 -06:00
|
|
|
config.ival(0) = 10;
|
2012-01-09 10:25:55 -07:00
|
|
|
config.ival(2) = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void computeRequest()
|
|
|
|
{
|
2012-12-02 04:31:43 -07:00
|
|
|
int size = curItemStock();
|
2012-01-09 10:25:55 -07:00
|
|
|
request_resume = (size <= goalCount()-goalGap());
|
|
|
|
request_suspend = (size >= goalCount());
|
|
|
|
}
|
2012-11-28 08:25:01 -07:00
|
|
|
|
|
|
|
static const size_t int28_size = PersistentDataItem::int28_size;
|
|
|
|
static const size_t hist_entry_size = PersistentDataItem::int28_size * 4;
|
|
|
|
|
|
|
|
size_t history_size() {
|
|
|
|
return history.data_size() / hist_entry_size;
|
|
|
|
}
|
2012-11-30 03:48:05 -07:00
|
|
|
int history_value(int idx, HistoryItem item) {
|
2012-11-28 08:25:01 -07:00
|
|
|
size_t hsize = history_size();
|
2012-11-30 03:48:05 -07:00
|
|
|
size_t base = ((history.ival(0)+1+idx) % hsize) * hist_entry_size;
|
|
|
|
return history.get_int28(base + item*int28_size);
|
2012-11-28 08:25:01 -07:00
|
|
|
}
|
2012-11-30 03:48:05 -07:00
|
|
|
int history_count(int idx) { return history_value(idx, HIST_COUNT); }
|
|
|
|
int history_amount(int idx) { return history_value(idx, HIST_AMOUNT); }
|
|
|
|
int history_inuse_count(int idx) { return history_value(idx, HIST_INUSE_COUNT); }
|
|
|
|
int history_inuse_amount(int idx) { return history_value(idx, HIST_INUSE_AMOUNT); }
|
2012-11-28 08:25:01 -07:00
|
|
|
|
|
|
|
void updateHistory()
|
|
|
|
{
|
|
|
|
size_t buffer_size = history_size();
|
2012-11-30 03:48:05 -07:00
|
|
|
if (buffer_size < MAX_HISTORY_SIZE && size_t(history.ival(0)+1) == buffer_size)
|
|
|
|
history.ensure_data(hist_entry_size*++buffer_size);
|
2012-11-28 08:25:01 -07:00
|
|
|
history.ival(0) = (history.ival(0)+1) % buffer_size;
|
|
|
|
|
|
|
|
size_t base = history.ival(0) * hist_entry_size;
|
|
|
|
|
2012-11-30 03:48:05 -07:00
|
|
|
history.set_int28(base + HIST_COUNT*int28_size, item_count);
|
|
|
|
history.set_int28(base + HIST_AMOUNT*int28_size, item_amount);
|
|
|
|
history.set_int28(base + HIST_INUSE_COUNT*int28_size, item_inuse_count);
|
|
|
|
history.set_int28(base + HIST_INUSE_AMOUNT*int28_size, item_inuse_amount);
|
2012-11-28 08:25:01 -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
|
|
|
|
2013-09-30 03:19:51 -06:00
|
|
|
DFHACK_PLUGIN_IS_ENABLED(enabled);
|
|
|
|
|
2012-01-09 06:29:28 -07:00
|
|
|
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 ||
|
2012-11-10 06:06:54 -07:00
|
|
|
job->job_type == job_type::CollectSand ||
|
|
|
|
job->job_type == job_type::MilkCreature ||
|
|
|
|
job->job_type == job_type::ShearCreature);
|
2012-01-09 06:29:28 -07:00
|
|
|
}
|
|
|
|
|
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);
|
2012-11-17 09:32:39 -07:00
|
|
|
static ItemConstraint *get_constraint(color_ostream &out, const std::string &str, PersistentDataItem *cfg = NULL, bool create = true);
|
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-03-10 04:55:42 -07:00
|
|
|
static void init_state(color_ostream &out)
|
2012-01-09 06:29:28 -07:00
|
|
|
{
|
2012-10-06 03:46:20 -06:00
|
|
|
config = World::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-10-06 03:46:20 -06:00
|
|
|
World::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());
|
2012-10-06 03:46:20 -06:00
|
|
|
World::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-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-01-09 06:29:28 -07:00
|
|
|
if (!config.isValid())
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
2012-10-06 03:46:20 -06:00
|
|
|
config = World::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-03-10 04:55:42 -07:00
|
|
|
out << "Enabling the plugin." << endl;
|
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-01-11 09:04:04 -07:00
|
|
|
/******************************
|
|
|
|
* JOB AUTO-RECOVERY *
|
|
|
|
******************************/
|
2012-01-09 06:29:28 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static void forget_job(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-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))
|
|
|
|
{
|
|
|
|
pj = new ProtectedJob(job);
|
|
|
|
assert(pj->holder);
|
|
|
|
known_jobs[pj->id] = pj;
|
|
|
|
}
|
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-01-11 09:04:04 -07:00
|
|
|
if ((++cnt % 5) != 0)
|
|
|
|
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-01-11 09:04:04 -07:00
|
|
|
if (pending_recover.size() != last_rlen || check_time)
|
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
|
|
|
|
if (check_time)
|
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-11-28 08:25:01 -07:00
|
|
|
|
|
|
|
for (size_t i = 0; i < constraints.size(); i++)
|
|
|
|
constraints[i]->updateHistory();
|
2012-01-09 08:20:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
/******************************
|
|
|
|
* ITEM COUNT CONSTRAINT *
|
|
|
|
******************************/
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-11-28 08:25:01 -07:00
|
|
|
static std::string history_key(PersistentDataItem &config) {
|
|
|
|
return stl_sprintf("workflow/history/%d", config.entry_id());
|
|
|
|
}
|
|
|
|
|
2012-11-17 09:32:39 -07:00
|
|
|
static ItemConstraint *get_constraint(color_ostream &out, const std::string &str, PersistentDataItem *cfg, bool create)
|
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;
|
|
|
|
|
|
|
|
int weight = 0;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item.subtype >= 0)
|
|
|
|
weight += 10000;
|
|
|
|
|
2012-11-17 09:32:39 -07:00
|
|
|
df::dfhack_material_category mat_mask(0);
|
2012-01-10 06:23:37 -07:00
|
|
|
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;
|
|
|
|
}
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2012-01-10 06:23:37 -07:00
|
|
|
if (mat_mask.whole != 0)
|
|
|
|
weight += 100;
|
2012-11-10 23:58:05 -07: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (material.type >= 0)
|
|
|
|
weight += (material.index >= 0 ? 5000 : 1000);
|
|
|
|
|
|
|
|
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-10 23:58:05 -07:00
|
|
|
item_quality::item_quality minqual = item_quality::Ordinary;
|
|
|
|
bool is_local = false;
|
|
|
|
std::string qualstr = vector_get(tokens, 3);
|
|
|
|
|
|
|
|
if(!qualstr.empty())
|
|
|
|
{
|
|
|
|
std::vector<std::string> qtokens;
|
|
|
|
split_string(&qtokens, qualstr, ",");
|
|
|
|
|
|
|
|
for (size_t i = 0; i < qtokens.size(); i++)
|
|
|
|
{
|
|
|
|
auto token = toLower(qtokens[i]);
|
|
|
|
|
|
|
|
if (token == "local")
|
|
|
|
is_local = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
FOR_ENUM_ITEMS(item_quality, qv)
|
|
|
|
{
|
|
|
|
if (toLower(ENUM_KEY_STR(item_quality, qv)) != token)
|
|
|
|
continue;
|
|
|
|
minqual = qv;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
out.printerr("Cannot parse token: %s\n", token.c_str());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_local || minqual > item_quality::Ordinary)
|
|
|
|
weight += 10;
|
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < constraints.size(); i++)
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
2012-01-09 08:20:28 -07:00
|
|
|
ItemConstraint *ct = constraints[i];
|
2012-03-14 02:09:02 -06:00
|
|
|
if (ct->is_craft == is_craft &&
|
|
|
|
ct->item == item && ct->material == material &&
|
2012-08-06 08:55:55 -06:00
|
|
|
ct->mat_mask.whole == mat_mask.whole &&
|
2012-11-10 23:58:05 -07:00
|
|
|
ct->min_quality == minqual &&
|
|
|
|
ct->is_local == is_local)
|
2012-01-09 08:20:28 -07:00
|
|
|
return ct;
|
2012-01-09 06:29:28 -07:00
|
|
|
}
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-11-17 09:32:39 -07:00
|
|
|
if (!create)
|
|
|
|
return NULL;
|
|
|
|
|
2012-01-09 08:20:28 -07:00
|
|
|
ItemConstraint *nct = new ItemConstraint;
|
2012-03-14 02:09:02 -06:00
|
|
|
nct->is_craft = is_craft;
|
2012-01-09 08:20:28 -07:00
|
|
|
nct->item = item;
|
|
|
|
nct->material = material;
|
|
|
|
nct->mat_mask = mat_mask;
|
2012-08-06 08:55:55 -06:00
|
|
|
nct->min_quality = minqual;
|
2012-11-10 23:58:05 -07:00
|
|
|
nct->is_local = is_local;
|
2012-01-09 08:20:28 -07:00
|
|
|
nct->weight = weight;
|
|
|
|
|
|
|
|
if (cfg)
|
|
|
|
nct->config = *cfg;
|
|
|
|
else
|
2012-01-09 06:29:28 -07:00
|
|
|
{
|
2012-10-06 03:46:20 -06:00
|
|
|
nct->config = World::AddPersistentData("workflow/constraints");
|
2012-01-09 10:25:55 -07:00
|
|
|
nct->init(str);
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
|
|
|
|
2012-11-28 08:25:01 -07:00
|
|
|
nct->history = World::GetPersistentData(history_key(nct->config), NULL);
|
|
|
|
|
2012-01-09 08:20:28 -07:00
|
|
|
constraints.push_back(nct);
|
|
|
|
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)
|
|
|
|
vector_erase_at(constraints, idx);
|
|
|
|
|
2012-10-06 03:46:20 -06:00
|
|
|
World::DeletePersistentData(cv->config);
|
2012-11-28 08:25:01 -07:00
|
|
|
World::DeletePersistentData(cv->history);
|
2012-01-09 08:20:28 -07:00
|
|
|
delete cv;
|
|
|
|
}
|
|
|
|
|
2012-10-23 11:42:03 -06:00
|
|
|
static bool deleteConstraint(std::string name)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < constraints.size(); i++)
|
|
|
|
{
|
|
|
|
if (constraints[i]->config.val() != name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
delete_constraint(constraints[i]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
bool is_craft = 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-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-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-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-25 02:44:23 -06:00
|
|
|
static int cbEnumJobOutputs(lua_State *L)
|
2012-01-10 06:23:37 -07:00
|
|
|
{
|
2012-10-25 02:44:23 -06:00
|
|
|
auto pj = (ProtectedJob*)lua_touserdata(L, lua_upvalueindex(1));
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-10-25 02:44:23 -06:00
|
|
|
lua_settop(L, 6);
|
2012-03-14 02:09:02 -06:00
|
|
|
|
2012-10-25 02:44:23 -06:00
|
|
|
df::dfhack_material_category mat_mask(0);
|
|
|
|
if (!lua_isnil(L, 3))
|
|
|
|
Lua::CheckDFAssign(L, &mat_mask, 3);
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-10-25 02:44:23 -06:00
|
|
|
link_job_constraint(
|
|
|
|
pj,
|
|
|
|
(df::item_type)luaL_optint(L, 1, -1), luaL_optint(L, 2, -1),
|
|
|
|
mat_mask, luaL_optint(L, 4, -1), luaL_optint(L, 5, -1),
|
|
|
|
lua_toboolean(L, 6)
|
|
|
|
);
|
2012-01-10 06:23:37 -07:00
|
|
|
|
2012-10-25 02:44:23 -06:00
|
|
|
return 0;
|
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
|
|
|
|
2012-10-25 02:44:23 -06:00
|
|
|
auto L = Lua::Core::State;
|
|
|
|
Lua::StackUnwinder frame(L);
|
|
|
|
|
|
|
|
bool ok = Lua::PushModulePublic(out, L, "plugins.workflow", "doEnumJobOutputs");
|
|
|
|
if (!ok)
|
|
|
|
out.printerr("The workflow lua module is not available.\n");
|
|
|
|
|
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-10-25 02:44:23 -06:00
|
|
|
if (!ok || !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-10-25 02:44:23 -06:00
|
|
|
// Call the lua module
|
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_pushlightuserdata(L, pj);
|
|
|
|
lua_pushcclosure(L, cbEnumJobOutputs, 1);
|
|
|
|
Lua::PushDFObject(L, pj->job_copy);
|
|
|
|
|
|
|
|
Lua::SafeCall(out, L, 2, 0);
|
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-11-12 07:27:58 -07:00
|
|
|
for (size_t i = 0; i < item->general_refs.size(); i++)
|
2012-01-11 09:04:04 -07:00
|
|
|
{
|
2012-11-12 07:27:58 -07:00
|
|
|
df::general_ref *ref = item->general_refs[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-11-12 07:27:58 -07:00
|
|
|
for (size_t i = 0; i < item->general_refs.size(); i++)
|
2012-01-12 09:07:53 -07:00
|
|
|
{
|
2012-11-12 07:27:58 -07:00
|
|
|
df::general_ref *ref = item->general_refs[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-10-25 03:20:41 -06:00
|
|
|
static bool isRouteVehicle(df::item *item)
|
|
|
|
{
|
|
|
|
int id = item->getVehicleID();
|
|
|
|
if (id < 0) return false;
|
|
|
|
|
|
|
|
auto vehicle = df::vehicle::find(id);
|
|
|
|
return vehicle && vehicle->route_id >= 0;
|
|
|
|
}
|
|
|
|
|
2012-11-08 10:27:56 -07:00
|
|
|
static bool isAssignedSquad(df::item *item)
|
|
|
|
{
|
|
|
|
auto &vec = ui->equipment.items_assigned[item->getType()];
|
|
|
|
return binsearch_index(vec, &df::item::id, item->id) >= 0;
|
|
|
|
}
|
|
|
|
|
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;
|
2012-11-28 08:25:01 -07:00
|
|
|
constraints[i]->item_inuse_amount = 0;
|
|
|
|
constraints[i]->item_inuse_count = 0;
|
2012-01-09 10:25:55 -07:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
#define F(x) bad_flags.bits.x = 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-11-28 11:46:56 -07:00
|
|
|
F(in_building); F(construction); F(artifact);
|
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-10-23 11:14:21 -06:00
|
|
|
std::vector<df::item*> &items = world->items.other[items_other_id::IN_PLAY];
|
2012-01-11 09:04:04 -07:00
|
|
|
|
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-11-16 14:33:36 -07:00
|
|
|
// don't count worn items
|
|
|
|
if (item->getWear() >= 1)
|
|
|
|
is_invalid = true;
|
2012-04-07 11:11:53 -06:00
|
|
|
|
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:
|
2012-10-26 13:53:18 -06:00
|
|
|
if (item->flags.bits.spider_web)
|
|
|
|
continue;
|
2012-01-27 10:17:50 -07:00
|
|
|
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-11-10 23:58:05 -07:00
|
|
|
|
|
|
|
if (cv->is_local && item->flags.bits.foreign)
|
|
|
|
continue;
|
|
|
|
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-10-25 03:20:41 -06:00
|
|
|
isRouteVehicle(item) ||
|
2012-01-12 09:07:53 -07:00
|
|
|
itemInRealJob(item) ||
|
2012-11-08 10:27:56 -07:00
|
|
|
itemBusy(item) ||
|
|
|
|
isAssignedSquad(item))
|
2012-01-10 06:23:37 -07:00
|
|
|
{
|
2012-11-08 10:27:56 -07:00
|
|
|
is_invalid = true;
|
2012-11-28 08:25:01 -07:00
|
|
|
cv->item_inuse_count++;
|
|
|
|
cv->item_inuse_amount += item->getStackSize();
|
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-01-09 10:25:55 -07:00
|
|
|
constraints[i]->computeRequest();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2012-12-02 04:31:43 -07:00
|
|
|
if (ct->low_stock_reported != DF_GLOBAL_VALUE(cur_season,-1))
|
|
|
|
{
|
|
|
|
int count = ct->goalCount(), gap = ct->goalGap();
|
|
|
|
|
|
|
|
if (count >= gap*3 && ct->curItemStock() < std::min(gap*2, (count-gap)/2))
|
|
|
|
{
|
|
|
|
ct->low_stock_reported = DF_GLOBAL_VALUE(cur_season,-1);
|
|
|
|
|
|
|
|
Gui::showAnnouncement("Stock level is low: " + info, COLOR_BROWN, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ct->low_stock_reported = -1;
|
|
|
|
}
|
|
|
|
|
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-10-23 11:42:03 -06:00
|
|
|
static void update_data_structures(color_ostream &out)
|
|
|
|
{
|
|
|
|
if (enabled) {
|
|
|
|
check_lost_jobs(out, 0);
|
|
|
|
recover_jobs(out);
|
|
|
|
update_job_data(out);
|
|
|
|
map_job_constraints(out);
|
|
|
|
map_job_items(out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************
|
|
|
|
* LUA API *
|
|
|
|
*************/
|
|
|
|
|
2013-09-30 03:19:51 -06:00
|
|
|
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable)
|
2012-10-23 11:42:03 -06:00
|
|
|
{
|
2013-09-30 03:19:51 -06:00
|
|
|
if (!Core::getInstance().isWorldLoaded()) {
|
|
|
|
out.printerr("World is not loaded: please load a game first.\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-10-23 11:42:03 -06:00
|
|
|
if (enable && !enabled)
|
|
|
|
{
|
|
|
|
enable_plugin(out);
|
|
|
|
}
|
|
|
|
else if (!enable && enabled)
|
|
|
|
{
|
|
|
|
enabled = false;
|
|
|
|
setOptionEnabled(CF_ENABLED, false);
|
|
|
|
stop_protect(out);
|
|
|
|
}
|
2013-09-30 03:19:51 -06:00
|
|
|
|
|
|
|
return CR_OK;
|
2012-10-23 11:42:03 -06:00
|
|
|
}
|
|
|
|
|
2012-11-30 03:48:05 -07:00
|
|
|
static void push_count_history(lua_State *L, ItemConstraint *icv)
|
|
|
|
{
|
|
|
|
size_t hsize = icv->history_size();
|
|
|
|
|
|
|
|
lua_createtable(L, hsize, 0);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < hsize; i++)
|
|
|
|
{
|
|
|
|
lua_createtable(L, 0, 4);
|
|
|
|
|
|
|
|
Lua::SetField(L, icv->history_amount(i), -1, "cur_amount");
|
|
|
|
Lua::SetField(L, icv->history_count(i), -1, "cur_count");
|
|
|
|
Lua::SetField(L, icv->history_inuse_amount(i), -1, "cur_in_use_amount");
|
|
|
|
Lua::SetField(L, icv->history_inuse_count(i), -1, "cur_in_use_count");
|
|
|
|
|
|
|
|
lua_rawseti(L, -2, i+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 11:42:03 -06:00
|
|
|
static void push_constraint(lua_State *L, ItemConstraint *cv)
|
|
|
|
{
|
|
|
|
lua_newtable(L);
|
|
|
|
int ctable = lua_gettop(L);
|
|
|
|
|
|
|
|
Lua::SetField(L, cv->config.entry_id(), ctable, "id");
|
|
|
|
Lua::SetField(L, cv->config.val(), ctable, "token");
|
|
|
|
|
|
|
|
// Constraint key
|
|
|
|
|
|
|
|
Lua::SetField(L, cv->item.type, ctable, "item_type");
|
|
|
|
Lua::SetField(L, cv->item.subtype, ctable, "item_subtype");
|
|
|
|
|
|
|
|
Lua::SetField(L, cv->is_craft, ctable, "is_craft");
|
|
|
|
|
2012-11-17 09:32:39 -07:00
|
|
|
lua_getglobal(L, "copyall");
|
2012-10-23 11:42:03 -06:00
|
|
|
Lua::PushDFObject(L, &cv->mat_mask);
|
2012-11-17 09:32:39 -07:00
|
|
|
lua_call(L, 1, 1);
|
2012-10-23 11:42:03 -06:00
|
|
|
lua_setfield(L, -2, "mat_mask");
|
|
|
|
|
|
|
|
Lua::SetField(L, cv->material.type, ctable, "mat_type");
|
|
|
|
Lua::SetField(L, cv->material.index, ctable, "mat_index");
|
|
|
|
|
|
|
|
Lua::SetField(L, (int)cv->min_quality, ctable, "min_quality");
|
2012-11-10 23:58:05 -07:00
|
|
|
Lua::SetField(L, (bool)cv->is_local, ctable, "is_local");
|
2012-10-23 11:42:03 -06:00
|
|
|
|
|
|
|
// Constraint value
|
|
|
|
|
|
|
|
Lua::SetField(L, cv->goalByCount(), ctable, "goal_by_count");
|
|
|
|
Lua::SetField(L, cv->goalCount(), ctable, "goal_value");
|
|
|
|
Lua::SetField(L, cv->goalGap(), ctable, "goal_gap");
|
|
|
|
|
|
|
|
Lua::SetField(L, cv->item_amount, ctable, "cur_amount");
|
|
|
|
Lua::SetField(L, cv->item_count, ctable, "cur_count");
|
2012-11-28 08:25:01 -07:00
|
|
|
Lua::SetField(L, cv->item_inuse_amount, ctable, "cur_in_use_amount");
|
|
|
|
Lua::SetField(L, cv->item_inuse_count, ctable, "cur_in_use_count");
|
2012-10-23 11:42:03 -06:00
|
|
|
|
|
|
|
// Current state value
|
|
|
|
|
|
|
|
if (cv->request_resume)
|
|
|
|
Lua::SetField(L, "resume", ctable, "request");
|
|
|
|
else if (cv->request_suspend)
|
|
|
|
Lua::SetField(L, "suspend", ctable, "request");
|
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
|
2012-11-30 03:48:05 -07:00
|
|
|
bool resumed = false, want_resumed = false;
|
|
|
|
|
2012-10-23 11:42:03 -06:00
|
|
|
for (size_t i = 0, j = 0; i < cv->jobs.size(); i++)
|
|
|
|
{
|
|
|
|
if (!cv->jobs[i]->isLive()) continue;
|
|
|
|
Lua::PushDFObject(L, cv->jobs[i]->actual_job);
|
|
|
|
lua_rawseti(L, -2, ++j);
|
2012-11-30 03:48:05 -07:00
|
|
|
|
|
|
|
if (cv->jobs[i]->want_resumed) {
|
|
|
|
want_resumed = true;
|
|
|
|
resumed = resumed || cv->jobs[i]->isActuallyResumed();
|
|
|
|
}
|
2012-10-23 11:42:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
lua_setfield(L, ctable, "jobs");
|
2012-11-30 03:48:05 -07:00
|
|
|
|
|
|
|
if (want_resumed && !resumed)
|
|
|
|
Lua::SetField(L, true, ctable, "is_delayed");
|
2012-10-23 11:42:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static int listConstraints(lua_State *L)
|
|
|
|
{
|
2012-11-30 03:48:05 -07:00
|
|
|
lua_settop(L, 2);
|
2012-10-23 11:42:03 -06:00
|
|
|
auto job = Lua::CheckDFObject<df::job>(L, 1);
|
2012-11-30 03:48:05 -07:00
|
|
|
bool with_history = lua_toboolean(L, 2);
|
2012-10-23 11:42:03 -06:00
|
|
|
|
2012-10-25 02:15:18 -06:00
|
|
|
lua_pushnil(L);
|
|
|
|
|
|
|
|
if (!enabled || (job && !isSupportedJob(job)))
|
2012-10-23 11:42:03 -06:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
color_ostream &out = *Lua::GetOutput(L);
|
|
|
|
update_data_structures(out);
|
|
|
|
|
2012-10-25 02:15:18 -06:00
|
|
|
ProtectedJob *pj = NULL;
|
|
|
|
if (job)
|
|
|
|
{
|
|
|
|
pj = get_known(job->id);
|
|
|
|
if (!pj)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-23 11:42:03 -06:00
|
|
|
lua_newtable(L);
|
|
|
|
|
|
|
|
auto &vec = (pj ? pj->constraints : constraints);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < vec.size(); i++)
|
|
|
|
{
|
|
|
|
push_constraint(L, vec[i]);
|
2012-11-30 03:48:05 -07:00
|
|
|
|
|
|
|
if (with_history)
|
|
|
|
{
|
|
|
|
push_count_history(L, vec[i]);
|
|
|
|
lua_setfield(L, -2, "history");
|
|
|
|
}
|
|
|
|
|
2012-10-23 11:42:03 -06:00
|
|
|
lua_rawseti(L, -2, i+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-11-17 09:32:39 -07:00
|
|
|
static int findConstraint(lua_State *L)
|
|
|
|
{
|
|
|
|
auto token = luaL_checkstring(L, 1);
|
|
|
|
|
|
|
|
color_ostream &out = *Lua::GetOutput(L);
|
|
|
|
update_data_structures(out);
|
|
|
|
|
|
|
|
ItemConstraint *icv = get_constraint(out, token, NULL, false);
|
|
|
|
|
|
|
|
if (icv)
|
|
|
|
push_constraint(L, icv);
|
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-23 11:42:03 -06:00
|
|
|
static int setConstraint(lua_State *L)
|
|
|
|
{
|
|
|
|
auto token = luaL_checkstring(L, 1);
|
|
|
|
bool by_count = lua_toboolean(L, 2);
|
2012-10-24 11:49:30 -06:00
|
|
|
int count = luaL_optint(L, 3, -1);
|
2012-10-23 11:42:03 -06:00
|
|
|
int gap = luaL_optint(L, 4, -1);
|
|
|
|
|
|
|
|
color_ostream &out = *Lua::GetOutput(L);
|
2012-11-17 09:32:39 -07:00
|
|
|
update_data_structures(out);
|
2012-10-23 11:42:03 -06:00
|
|
|
|
|
|
|
ItemConstraint *icv = get_constraint(out, token);
|
|
|
|
if (!icv)
|
|
|
|
luaL_error(L, "invalid constraint: %s", token);
|
|
|
|
|
2012-10-25 02:15:18 -06:00
|
|
|
if (!lua_isnil(L, 2))
|
|
|
|
icv->setGoalByCount(by_count);
|
2012-10-24 11:49:30 -06:00
|
|
|
if (!lua_isnil(L, 3))
|
|
|
|
icv->setGoalCount(count);
|
|
|
|
if (!lua_isnil(L, 4))
|
|
|
|
icv->setGoalGap(gap);
|
2012-10-23 11:42:03 -06:00
|
|
|
|
|
|
|
process_constraints(out);
|
|
|
|
push_constraint(L, icv);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-11-28 08:25:01 -07:00
|
|
|
static int getCountHistory(lua_State *L)
|
|
|
|
{
|
|
|
|
auto token = luaL_checkstring(L, 1);
|
|
|
|
|
|
|
|
color_ostream &out = *Lua::GetOutput(L);
|
|
|
|
update_data_structures(out);
|
|
|
|
|
|
|
|
ItemConstraint *icv = get_constraint(out, token, NULL, false);
|
|
|
|
|
|
|
|
if (icv)
|
2012-11-30 03:48:05 -07:00
|
|
|
push_count_history(L, icv);
|
2012-11-28 08:25:01 -07:00
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-23 11:42:03 -06:00
|
|
|
DFHACK_PLUGIN_LUA_FUNCTIONS {
|
|
|
|
DFHACK_LUA_FUNCTION(deleteConstraint),
|
|
|
|
DFHACK_LUA_END
|
|
|
|
};
|
|
|
|
|
|
|
|
DFHACK_PLUGIN_LUA_COMMANDS {
|
|
|
|
DFHACK_LUA_COMMAND(listConstraints),
|
2012-11-17 09:32:39 -07:00
|
|
|
DFHACK_LUA_COMMAND(findConstraint),
|
2012-10-23 11:42:03 -06:00
|
|
|
DFHACK_LUA_COMMAND(setConstraint),
|
2012-11-28 08:25:01 -07:00
|
|
|
DFHACK_LUA_COMMAND(getCountHistory),
|
2012-10-23 11:42:03 -06:00
|
|
|
DFHACK_LUA_END
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2012-11-28 08:25:01 -07:00
|
|
|
if (cv->item_count || cv->item_inuse_count)
|
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, "
|
2012-11-28 08:25:01 -07:00
|
|
|
<< cv->item_inuse_count << " in use." << endl;
|
2012-01-10 06:23:37 -07:00
|
|
|
|
|
|
|
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-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
|
|
|
CoreSuspender suspend;
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
if (!Core::getInstance().isWorldLoaded()) {
|
|
|
|
out.printerr("World is not loaded: please load a game first.\n");
|
2012-01-28 05:03:56 -07:00
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-10-23 11:42:03 -06:00
|
|
|
update_data_structures(out);
|
2012-01-07 10:47:23 -07:00
|
|
|
|
|
|
|
df::building *workshop = NULL;
|
2012-08-12 23:39:11 -06:00
|
|
|
//FIXME: unused variable!
|
|
|
|
//df::job *job = NULL;
|
2012-01-07 10:47:23 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
if (Gui::dwarfmode_hotkey(Core::getTopViewscreen()) &&
|
2012-01-07 10:47:23 -07:00
|
|
|
ui->main.mode == ui_sidebar_mode::QueryBuilding)
|
|
|
|
{
|
|
|
|
workshop = world->selected_building;
|
2012-08-12 23:39:11 -06:00
|
|
|
//job = Gui::getSelectedWorkshopJob(out, true);
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
|
|
|
|
2012-01-09 08:20:28 -07:00
|
|
|
std::string cmd = parameters.empty() ? "list" : parameters[0];
|
2012-01-09 06:29:28 -07:00
|
|
|
|
2012-01-12 09:07:53 -07:00
|
|
|
if (cmd == "enable" || cmd == "disable")
|
2012-01-09 06:29:28 -07:00
|
|
|
{
|
2012-01-12 09:07:53 -07:00
|
|
|
bool enable = (cmd == "enable");
|
2012-10-23 11:42:03 -06:00
|
|
|
if (enable)
|
2013-09-30 03:19:51 -06:00
|
|
|
plugin_enable(out, true);
|
2012-10-23 11:42:03 -06:00
|
|
|
else if (parameters.size() == 1)
|
2012-01-09 06:29:28 -07:00
|
|
|
{
|
2013-09-30 03:19:51 -06:00
|
|
|
plugin_enable(out, false);
|
2012-01-12 09:07:53 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
out << "The plugin is disabled." << endl;
|
2012-01-09 06:29:28 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 1; i < parameters.size(); i++)
|
2012-01-09 06:29:28 -07:00
|
|
|
{
|
2012-01-12 09:07:53 -07:00
|
|
|
if (parameters[i] == "drybuckets")
|
|
|
|
setOptionEnabled(CF_DRYBUCKETS, enable);
|
|
|
|
else if (parameters[i] == "auto-melt")
|
|
|
|
setOptionEnabled(CF_AUTOMELT, enable);
|
|
|
|
else
|
|
|
|
return CR_WRONG_USAGE;
|
2012-01-09 06:29:28 -07:00
|
|
|
}
|
|
|
|
|
2012-01-12 09:07:53 -07:00
|
|
|
if (enabled)
|
2012-03-10 04:55:42 -07:00
|
|
|
out << "The plugin is enabled." << endl;
|
2012-01-12 09:07:53 -07:00
|
|
|
else
|
2012-03-10 04:55:42 -07:00
|
|
|
out << "The plugin is disabled." << endl;
|
2012-01-12 09:07:53 -07:00
|
|
|
|
|
|
|
if (isOptionEnabled(CF_DRYBUCKETS))
|
2012-03-10 04:55:42 -07:00
|
|
|
out << "Option drybuckets is enabled." << endl;
|
2012-01-12 09:07:53 -07:00
|
|
|
if (isOptionEnabled(CF_AUTOMELT))
|
2012-03-10 04:55:42 -07:00
|
|
|
out << "Option auto-melt is enabled." << endl;
|
2012-01-12 09:07:53 -07:00
|
|
|
|
2012-01-09 06:29:28 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
2012-01-11 09:04:04 -07:00
|
|
|
else if (cmd == "count" || cmd == "amount")
|
2012-01-09 10:25:55 -07:00
|
|
|
{
|
|
|
|
if (!enabled)
|
2012-03-10 04:55:42 -07:00
|
|
|
enable_plugin(out);
|
2012-01-09 10:25:55 -07:00
|
|
|
}
|
2012-01-09 06:29:28 -07:00
|
|
|
|
|
|
|
if (!enabled)
|
2013-09-30 03:19:51 -06:00
|
|
|
{
|
|
|
|
out.printerr("Error: the plugin is not enabled.\n");
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
2012-01-09 06:29:28 -07:00
|
|
|
|
2012-01-09 08:20:28 -07:00
|
|
|
if (cmd == "jobs")
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
|
|
|
if (workshop)
|
|
|
|
{
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < workshop->jobs.size(); i++)
|
2012-03-10 04:55:42 -07:00
|
|
|
print_job(out, get_known(workshop->jobs[i]->id));
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (TKnownJobs::iterator it = known_jobs.begin(); it != known_jobs.end(); ++it)
|
2012-01-11 09:04:04 -07:00
|
|
|
if (it->second->isLive())
|
2012-03-10 04:55:42 -07:00
|
|
|
print_job(out, it->second);
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool pending = false;
|
|
|
|
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < pending_recover.size(); i++)
|
2012-01-07 10:47:23 -07:00
|
|
|
{
|
|
|
|
if (!workshop || pending_recover[i]->holder == workshop)
|
|
|
|
{
|
|
|
|
if (!pending)
|
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
out.print("\nPending recovery:\n");
|
2012-01-07 10:47:23 -07:00
|
|
|
pending = true;
|
|
|
|
}
|
|
|
|
|
2012-04-10 01:43:36 -06:00
|
|
|
Job::printJobDetails(out, pending_recover[i]->job_copy);
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-09 06:29:28 -07:00
|
|
|
return CR_OK;
|
2012-01-07 10:47:23 -07:00
|
|
|
}
|
2012-01-09 08:20:28 -07:00
|
|
|
else if (cmd == "list")
|
|
|
|
{
|
2012-01-31 09:55:38 -07:00
|
|
|
for (size_t i = 0; i < constraints.size(); i++)
|
2012-03-10 04:55:42 -07:00
|
|
|
print_constraint(out, constraints[i]);
|
2012-01-09 08:20:28 -07:00
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
2012-03-14 02:09:02 -06:00
|
|
|
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;
|
|
|
|
}
|
2012-01-11 09:04:04 -07:00
|
|
|
else if (cmd == "count" || cmd == "amount")
|
2012-01-09 08:20:28 -07:00
|
|
|
{
|
|
|
|
if (parameters.size() < 3)
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
|
|
|
int limit = atoi(parameters[2].c_str());
|
|
|
|
if (limit <= 0) {
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr("Invalid limit value.\n");
|
2012-01-09 08:20:28 -07:00
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
ItemConstraint *icv = get_constraint(out, parameters[1]);
|
2012-01-09 08:20:28 -07:00
|
|
|
if (!icv)
|
|
|
|
return CR_FAILURE;
|
|
|
|
|
2012-01-11 09:04:04 -07:00
|
|
|
icv->setGoalByCount(cmd == "count");
|
2012-01-09 08:20:28 -07:00
|
|
|
icv->setGoalCount(limit);
|
|
|
|
if (parameters.size() >= 4)
|
|
|
|
icv->setGoalGap(atoi(parameters[3].c_str()));
|
2012-01-10 06:23:37 -07:00
|
|
|
else
|
|
|
|
icv->setGoalGap(-1);
|
2012-01-09 08:20:28 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
process_constraints(out);
|
|
|
|
print_constraint(out, icv);
|
2012-01-09 08:20:28 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
else if (cmd == "unlimit")
|
|
|
|
{
|
|
|
|
if (parameters.size() != 2)
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
2012-10-23 11:42:03 -06:00
|
|
|
if (deleteConstraint(parameters[1]))
|
2012-01-09 08:20:28 -07:00
|
|
|
return CR_OK;
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr("Constraint not found: %s\n", parameters[1].c_str());
|
2012-01-09 08:20:28 -07:00
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
2012-03-14 02:09:02 -06:00
|
|
|
else if (cmd == "unlimit-all")
|
2012-03-13 18:52:58 -06:00
|
|
|
{
|
2012-03-14 02:09:02 -06:00
|
|
|
if (parameters.size() != 1)
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
|
|
|
while (!constraints.empty())
|
2012-03-15 07:43:05 -06:00
|
|
|
delete_constraint(constraints[0]);
|
2012-03-14 02:09:02 -06:00
|
|
|
|
|
|
|
out.print("Removed all constraints.\n");
|
|
|
|
return CR_OK;
|
2012-03-12 01:05:22 -06:00
|
|
|
}
|
2012-01-07 10:47:23 -07:00
|
|
|
else
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|