2013-03-25 04:47:05 -06:00
|
|
|
#include "uicommon.h"
|
|
|
|
|
|
|
|
#include "modules/Gui.h"
|
|
|
|
|
|
|
|
#include "df/world.h"
|
|
|
|
#include "df/world_raws.h"
|
|
|
|
#include "df/building_def.h"
|
|
|
|
#include "df/viewscreen_dwarfmodest.h"
|
2014-05-03 04:56:34 -06:00
|
|
|
#include "df/viewscreen_tradegoodsst.h"
|
2013-03-25 04:47:05 -06:00
|
|
|
#include "df/building_stockpilest.h"
|
2014-09-07 14:03:51 -06:00
|
|
|
#include "modules/Buildings.h"
|
2013-03-25 04:47:05 -06:00
|
|
|
#include "modules/Items.h"
|
|
|
|
#include "df/building_tradedepotst.h"
|
|
|
|
#include "df/general_ref_building_holderst.h"
|
|
|
|
#include "df/job.h"
|
|
|
|
#include "df/job_item_ref.h"
|
|
|
|
#include "modules/Job.h"
|
|
|
|
#include "df/ui.h"
|
2013-03-31 04:25:57 -06:00
|
|
|
#include "df/mandate.h"
|
2013-03-25 04:47:05 -06:00
|
|
|
#include "modules/Maps.h"
|
|
|
|
|
|
|
|
using df::building_stockpilest;
|
|
|
|
|
|
|
|
DFHACK_PLUGIN("autotrade");
|
2015-07-28 19:48:00 -06:00
|
|
|
REQUIRE_GLOBAL(gps);
|
2014-12-02 19:44:20 -07:00
|
|
|
REQUIRE_GLOBAL(world);
|
|
|
|
REQUIRE_GLOBAL(cursor);
|
|
|
|
REQUIRE_GLOBAL(ui);
|
2013-03-25 04:47:05 -06:00
|
|
|
|
2014-05-03 04:56:34 -06:00
|
|
|
static const string PERSISTENCE_KEY = "autotrade/stockpiles";
|
2013-03-25 04:47:05 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Depot Access
|
|
|
|
*/
|
|
|
|
|
|
|
|
class TradeDepotInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TradeDepotInfo() : depot(0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool findDepot()
|
|
|
|
{
|
|
|
|
if (isValid())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
reset();
|
|
|
|
for(auto bld_it = world->buildings.all.begin(); bld_it != world->buildings.all.end(); bld_it++)
|
|
|
|
{
|
|
|
|
auto bld = *bld_it;
|
|
|
|
if (!isUsableDepot(bld))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
depot = bld;
|
|
|
|
id = depot->id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return depot;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool assignItem(df::item *item)
|
|
|
|
{
|
|
|
|
auto href = df::allocate<df::general_ref_building_holderst>();
|
|
|
|
if (!href)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto job = new df::job();
|
|
|
|
|
|
|
|
df::coord tpos(depot->centerx, depot->centery, depot->z);
|
|
|
|
job->pos = tpos;
|
|
|
|
|
|
|
|
job->job_type = job_type::BringItemToDepot;
|
|
|
|
|
|
|
|
// job <-> item link
|
|
|
|
if (!Job::attachJobItem(job, item, df::job_item_ref::Hauled))
|
|
|
|
{
|
|
|
|
delete job;
|
|
|
|
delete href;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// job <-> building link
|
|
|
|
href->building_id = id;
|
|
|
|
depot->jobs.push_back(job);
|
|
|
|
job->general_refs.push_back(href);
|
|
|
|
|
|
|
|
// add to job list
|
|
|
|
Job::linkIntoWorld(job);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-01 01:02:39 -06:00
|
|
|
void reset()
|
2013-03-25 04:47:05 -06:00
|
|
|
{
|
|
|
|
depot = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int32_t id;
|
|
|
|
df::building *depot;
|
|
|
|
|
|
|
|
bool isUsableDepot(df::building* bld)
|
|
|
|
{
|
|
|
|
if (bld->getType() != building_type::TradeDepot)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (bld->getBuildStage() < bld->getMaxBuildStage())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (bld->jobs.size() == 1 && bld->jobs[0]->job_type == job_type::DestroyBuilding)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isValid()
|
|
|
|
{
|
|
|
|
if (!depot)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto found = df::building::find(id);
|
|
|
|
return found && found == depot && isUsableDepot(found);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
static TradeDepotInfo depot_info;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Item Manipulation
|
|
|
|
*/
|
|
|
|
|
|
|
|
static bool is_valid_item(df::item *item)
|
|
|
|
{
|
2018-05-12 09:51:36 -06:00
|
|
|
// Similar to Items::canTrade() with a few checks changed
|
2013-03-25 04:47:05 -06:00
|
|
|
for (size_t i = 0; i < item->general_refs.size(); i++)
|
|
|
|
{
|
|
|
|
df::general_ref *ref = item->general_refs[i];
|
|
|
|
|
|
|
|
switch (ref->getType())
|
|
|
|
{
|
|
|
|
case general_ref_type::CONTAINED_IN_ITEM:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case general_ref_type::UNIT_HOLDER:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case general_ref_type::BUILDING_HOLDER:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < item->specific_refs.size(); i++)
|
|
|
|
{
|
|
|
|
df::specific_ref *ref = item->specific_refs[i];
|
|
|
|
|
|
|
|
if (ref->type == specific_ref_type::JOB)
|
|
|
|
{
|
|
|
|
// Ignore any items assigned to a job
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-12 09:51:36 -06:00
|
|
|
if (!Items::checkMandates(item))
|
2013-03-31 04:25:57 -06:00
|
|
|
return false;
|
2013-03-25 04:47:05 -06:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-03 04:56:34 -06:00
|
|
|
static void mark_all_in_stockpiles(vector<PersistentStockpileInfo> &stockpiles)
|
2013-03-25 04:47:05 -06:00
|
|
|
{
|
|
|
|
if (!depot_info.findDepot())
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// Precompute a bitmask with the bad flags
|
|
|
|
df::item_flags bad_flags;
|
|
|
|
bad_flags.whole = 0;
|
|
|
|
|
|
|
|
#define F(x) bad_flags.bits.x = true;
|
|
|
|
F(dump); F(forbid); F(garbage_collect);
|
|
|
|
F(hostile); F(on_fire); F(rotten); F(trader);
|
|
|
|
F(in_building); F(construction); F(artifact);
|
|
|
|
F(spider_web); F(owned); F(in_job);
|
|
|
|
#undef F
|
|
|
|
|
|
|
|
size_t marked_count = 0;
|
|
|
|
size_t error_count = 0;
|
2014-09-07 14:03:51 -06:00
|
|
|
for (auto it = stockpiles.begin(); it != stockpiles.end(); it++)
|
2013-03-25 04:47:05 -06:00
|
|
|
{
|
2014-09-07 14:03:51 -06:00
|
|
|
if (!it->isValid())
|
2013-03-25 04:47:05 -06:00
|
|
|
continue;
|
|
|
|
|
2014-09-07 14:03:51 -06:00
|
|
|
Buildings::StockpileIterator stored;
|
|
|
|
for (stored.begin(it->getStockpile()); !stored.done(); ++stored)
|
2013-03-25 04:47:05 -06:00
|
|
|
{
|
2014-09-07 14:03:51 -06:00
|
|
|
df::item *item = *stored;
|
|
|
|
if (item->flags.whole & bad_flags.whole)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!is_valid_item(item))
|
2013-03-25 04:47:05 -06:00
|
|
|
continue;
|
|
|
|
|
2013-03-31 04:25:57 -06:00
|
|
|
// In case of container, check contained items for mandates
|
|
|
|
bool mandates_ok = true;
|
|
|
|
vector<df::item*> contained_items;
|
|
|
|
Items::getContainedItems(item, &contained_items);
|
2018-05-12 09:51:36 -06:00
|
|
|
for (df::item *cit : contained_items)
|
2013-03-31 04:25:57 -06:00
|
|
|
{
|
2018-05-12 09:51:36 -06:00
|
|
|
if (!Items::checkMandates(cit))
|
2013-03-31 04:25:57 -06:00
|
|
|
{
|
|
|
|
mandates_ok = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mandates_ok)
|
|
|
|
continue;
|
|
|
|
|
2013-03-25 04:47:05 -06:00
|
|
|
if (depot_info.assignItem(item))
|
|
|
|
{
|
|
|
|
++marked_count;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (++error_count < 5)
|
|
|
|
{
|
|
|
|
Gui::showZoomAnnouncement(df::announcement_type::CANCEL_JOB, item->pos,
|
|
|
|
"Cannot trade item from stockpile " + int_to_string(it->getId()), COLOR_RED, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (marked_count)
|
|
|
|
Gui::showAnnouncement("Marked " + int_to_string(marked_count) + " items for trade", COLOR_GREEN, false);
|
|
|
|
|
|
|
|
if (error_count >= 5)
|
|
|
|
{
|
|
|
|
Gui::showAnnouncement(int_to_string(error_count) + " items were not marked", COLOR_RED, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stockpile Monitoring
|
|
|
|
*/
|
|
|
|
|
|
|
|
class StockpileMonitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool isMonitored(df::building_stockpilest *sp)
|
|
|
|
{
|
|
|
|
for (auto it = monitored_stockpiles.begin(); it != monitored_stockpiles.end(); it++)
|
|
|
|
{
|
|
|
|
if (it->matches(sp))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(df::building_stockpilest *sp)
|
|
|
|
{
|
2014-05-03 04:56:34 -06:00
|
|
|
auto pile = PersistentStockpileInfo(sp, PERSISTENCE_KEY);
|
2013-03-25 04:47:05 -06:00
|
|
|
if (pile.isValid())
|
|
|
|
{
|
2014-05-03 04:56:34 -06:00
|
|
|
monitored_stockpiles.push_back(pile);
|
2013-03-25 04:47:05 -06:00
|
|
|
monitored_stockpiles.back().save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove(df::building_stockpilest *sp)
|
|
|
|
{
|
|
|
|
for (auto it = monitored_stockpiles.begin(); it != monitored_stockpiles.end(); it++)
|
|
|
|
{
|
|
|
|
if (it->matches(sp))
|
|
|
|
{
|
|
|
|
it->remove();
|
|
|
|
monitored_stockpiles.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void doCycle()
|
|
|
|
{
|
|
|
|
if (!can_trade())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (auto it = monitored_stockpiles.begin(); it != monitored_stockpiles.end();)
|
|
|
|
{
|
|
|
|
if (!it->isValid())
|
|
|
|
{
|
|
|
|
it = monitored_stockpiles.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
2014-05-03 04:56:34 -06:00
|
|
|
mark_all_in_stockpiles(monitored_stockpiles);
|
2013-03-25 04:47:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
monitored_stockpiles.clear();
|
|
|
|
std::vector<PersistentDataItem> items;
|
2014-05-03 04:56:34 -06:00
|
|
|
DFHack::World::GetPersistentData(&items, PERSISTENCE_KEY);
|
2013-03-25 04:47:05 -06:00
|
|
|
|
|
|
|
for (auto i = items.begin(); i != items.end(); i++)
|
|
|
|
{
|
2014-05-03 04:56:34 -06:00
|
|
|
auto pile = PersistentStockpileInfo(*i, PERSISTENCE_KEY);
|
2013-03-25 04:47:05 -06:00
|
|
|
if (pile.load())
|
2014-05-03 04:56:34 -06:00
|
|
|
monitored_stockpiles.push_back(pile);
|
2013-03-25 04:47:05 -06:00
|
|
|
else
|
|
|
|
pile.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2014-05-03 04:56:34 -06:00
|
|
|
vector<PersistentStockpileInfo> monitored_stockpiles;
|
2013-03-25 04:47:05 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
static StockpileMonitor monitor;
|
|
|
|
|
|
|
|
#define DELTA_TICKS 600
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_onupdate ( color_ostream &out )
|
|
|
|
{
|
|
|
|
if(!Maps::IsValid())
|
|
|
|
return CR_OK;
|
|
|
|
|
|
|
|
if (DFHack::World::ReadPauseState())
|
|
|
|
return CR_OK;
|
|
|
|
|
2018-05-29 21:24:02 -06:00
|
|
|
if (world->frame_counter % DELTA_TICKS != 0)
|
2013-03-25 04:47:05 -06:00
|
|
|
return CR_OK;
|
|
|
|
|
|
|
|
monitor.doCycle();
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct trade_hook : public df::viewscreen_dwarfmodest
|
|
|
|
{
|
|
|
|
typedef df::viewscreen_dwarfmodest interpose_base;
|
|
|
|
|
|
|
|
bool handleInput(set<df::interface_key> *input)
|
|
|
|
{
|
2018-05-09 08:25:55 -06:00
|
|
|
if (Gui::inRenameBuilding())
|
|
|
|
return false;
|
|
|
|
|
2013-03-25 04:47:05 -06:00
|
|
|
building_stockpilest *sp = get_selected_stockpile();
|
|
|
|
if (!sp)
|
|
|
|
return false;
|
|
|
|
|
2014-05-03 04:56:34 -06:00
|
|
|
if (input->count(interface_key::CUSTOM_SHIFT_T))
|
2013-03-25 04:47:05 -06:00
|
|
|
{
|
|
|
|
if (monitor.isMonitored(sp))
|
|
|
|
monitor.remove(sp);
|
|
|
|
else
|
|
|
|
monitor.add(sp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_VMETHOD_INTERPOSE(void, feed, (set<df::interface_key> *input))
|
|
|
|
{
|
|
|
|
if (!handleInput(input))
|
|
|
|
INTERPOSE_NEXT(feed)(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_VMETHOD_INTERPOSE(void, render, ())
|
|
|
|
{
|
|
|
|
INTERPOSE_NEXT(render)();
|
|
|
|
|
|
|
|
building_stockpilest *sp = get_selected_stockpile();
|
|
|
|
if (!sp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto dims = Gui::getDwarfmodeViewDims();
|
|
|
|
int left_margin = dims.menu_x1 + 1;
|
|
|
|
int x = left_margin;
|
2014-09-09 20:51:58 -06:00
|
|
|
int y = dims.y2 - 5;
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-08-19 14:19:13 -06:00
|
|
|
int links = 0;
|
|
|
|
links += sp->links.give_to_pile.size();
|
|
|
|
links += sp->links.take_from_pile.size();
|
|
|
|
links += sp->links.give_to_workshop.size();
|
|
|
|
links += sp->links.take_from_workshop.size();
|
2014-09-09 21:08:38 -06:00
|
|
|
bool state = monitor.isMonitored(sp);
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2014-09-09 21:08:38 -06:00
|
|
|
if (links + 12 >= y) {
|
|
|
|
y = dims.y2;
|
|
|
|
OutputString(COLOR_WHITE, x, y, "Auto: ");
|
2014-11-15 14:16:32 -07:00
|
|
|
x += 11;
|
2014-09-09 21:08:38 -06:00
|
|
|
OutputString(COLOR_LIGHTRED, x, y, "T");
|
|
|
|
OutputString(state? COLOR_LIGHTGREEN: COLOR_GREY, x, y, "rade ");
|
|
|
|
} else {
|
|
|
|
OutputToggleString(x, y, "Auto trade", "T", state, true, left_margin, COLOR_WHITE, COLOR_LIGHTRED);
|
|
|
|
}
|
2013-03-25 04:47:05 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
IMPLEMENT_VMETHOD_INTERPOSE(trade_hook, feed);
|
|
|
|
IMPLEMENT_VMETHOD_INTERPOSE(trade_hook, render);
|
|
|
|
|
2014-05-03 04:56:34 -06:00
|
|
|
|
2013-03-25 04:47:05 -06:00
|
|
|
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
|
|
|
|
{
|
|
|
|
switch (event)
|
|
|
|
{
|
|
|
|
case DFHack::SC_MAP_LOADED:
|
|
|
|
depot_info.reset();
|
|
|
|
monitor.reset();
|
|
|
|
break;
|
|
|
|
case DFHack::SC_MAP_UNLOADED:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-30 03:19:51 -06:00
|
|
|
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable)
|
2013-03-25 04:47:05 -06:00
|
|
|
{
|
2013-09-30 03:19:51 -06:00
|
|
|
if (enable != is_enabled)
|
|
|
|
{
|
|
|
|
depot_info.reset();
|
|
|
|
monitor.reset();
|
|
|
|
|
|
|
|
if (!INTERPOSE_HOOK(trade_hook, feed).apply(enable) ||
|
2014-11-06 21:47:14 -07:00
|
|
|
!INTERPOSE_HOOK(trade_hook, render).apply(enable))
|
2013-09-30 03:19:51 -06:00
|
|
|
return CR_FAILURE;
|
2013-03-25 04:47:05 -06:00
|
|
|
|
2013-09-30 03:19:51 -06:00
|
|
|
is_enabled = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
|
|
|
|
{
|
2013-03-25 04:47:05 -06:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
|
|
|
|
{
|
|
|
|
return CR_OK;
|
|
|
|
}
|