2014-12-23 14:56:15 -07:00
|
|
|
#include "buildingplan-lib.h"
|
2013-01-07 01:17:38 -07:00
|
|
|
|
|
|
|
DFHACK_PLUGIN("buildingplan");
|
2014-06-14 05:50:47 -06:00
|
|
|
#define PLUGIN_VERSION 0.14
|
2014-12-02 19:44:20 -07:00
|
|
|
REQUIRE_GLOBAL(ui);
|
|
|
|
REQUIRE_GLOBAL(ui_build_selector);
|
|
|
|
REQUIRE_GLOBAL(world);
|
2013-02-01 06:22:06 -07:00
|
|
|
|
2013-02-15 16:10:47 -07:00
|
|
|
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
|
2013-01-18 23:20:37 -07:00
|
|
|
{
|
2013-02-15 16:10:47 -07:00
|
|
|
return CR_OK;
|
2013-01-18 23:20:37 -07:00
|
|
|
}
|
|
|
|
|
2013-10-02 09:55:48 -06:00
|
|
|
|
2013-01-07 01:17:38 -07:00
|
|
|
static bool is_planmode_enabled(df::building_type type)
|
|
|
|
{
|
|
|
|
if (planmode_enabled.find(type) == planmode_enabled.end())
|
|
|
|
{
|
2013-02-08 20:49:36 -07:00
|
|
|
return false;
|
2013-01-07 01:17:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return planmode_enabled[type];
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DAY_TICKS 1200
|
|
|
|
DFhackCExport command_result plugin_onupdate(color_ostream &out)
|
|
|
|
{
|
|
|
|
static decltype(world->frame_counter) last_frame_count = 0;
|
|
|
|
if ((world->frame_counter - last_frame_count) >= DAY_TICKS/2)
|
|
|
|
{
|
|
|
|
last_frame_count = world->frame_counter;
|
|
|
|
planner.doCycle();
|
2014-06-14 05:50:47 -06:00
|
|
|
roomMonitor.doCycle();
|
2013-01-07 01:17:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//START Viewscreen Hook
|
|
|
|
struct buildingplan_hook : public df::viewscreen_dwarfmodest
|
|
|
|
{
|
|
|
|
//START UI Methods
|
|
|
|
typedef df::viewscreen_dwarfmodest interpose_base;
|
|
|
|
|
|
|
|
void send_key(const df::interface_key &key)
|
|
|
|
{
|
|
|
|
set< df::interface_key > keys;
|
|
|
|
keys.insert(key);
|
|
|
|
this->feed(&keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isInPlannedBuildingQueryMode()
|
|
|
|
{
|
2015-02-14 20:53:06 -07:00
|
|
|
return (ui->main.mode == df::ui_sidebar_mode::QueryBuilding ||
|
2013-01-07 01:17:38 -07:00
|
|
|
ui->main.mode == df::ui_sidebar_mode::BuildingItems) &&
|
2013-01-18 23:20:37 -07:00
|
|
|
planner.getSelectedPlannedBuilding();
|
2013-01-07 01:17:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isInPlannedBuildingPlacementMode()
|
|
|
|
{
|
|
|
|
return ui->main.mode == ui_sidebar_mode::Build &&
|
|
|
|
ui_build_selector &&
|
|
|
|
ui_build_selector->stage < 2 &&
|
|
|
|
planner.isPlanableBuilding(ui_build_selector->building_type);
|
|
|
|
}
|
|
|
|
|
2014-06-14 05:50:47 -06:00
|
|
|
std::vector<Units::NoblePosition> getNoblePositionOfSelectedBuildingOwner()
|
|
|
|
{
|
|
|
|
std::vector<Units::NoblePosition> np;
|
|
|
|
if (ui->main.mode != df::ui_sidebar_mode::QueryBuilding ||
|
|
|
|
!world->selected_building ||
|
|
|
|
!world->selected_building->owner)
|
|
|
|
{
|
|
|
|
return np;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (world->selected_building->getType())
|
|
|
|
{
|
|
|
|
case building_type::Bed:
|
|
|
|
case building_type::Chair:
|
|
|
|
case building_type::Table:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return np;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getUniqueNoblePositions(world->selected_building->owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isInNobleRoomQueryMode()
|
|
|
|
{
|
|
|
|
if (getNoblePositionOfSelectedBuildingOwner().size() > 0)
|
|
|
|
return canReserveRoom(world->selected_building);
|
2015-02-14 20:53:06 -07:00
|
|
|
else
|
2014-06-14 05:50:47 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-18 23:20:37 -07:00
|
|
|
bool handleInput(set<df::interface_key> *input)
|
2013-01-07 01:17:38 -07:00
|
|
|
{
|
|
|
|
if (isInPlannedBuildingPlacementMode())
|
|
|
|
{
|
|
|
|
auto type = ui_build_selector->building_type;
|
2014-05-04 04:23:10 -06:00
|
|
|
if (input->count(interface_key::CUSTOM_SHIFT_P))
|
2013-01-07 01:17:38 -07:00
|
|
|
{
|
|
|
|
planmode_enabled[type] = !planmode_enabled[type];
|
|
|
|
if (!planmode_enabled[type])
|
|
|
|
{
|
|
|
|
send_key(interface_key::CURSOR_DOWN_Z);
|
|
|
|
send_key(interface_key::CURSOR_UP_Z);
|
2013-02-07 04:04:52 -07:00
|
|
|
planner.in_dummmy_screen = false;
|
2013-01-07 01:17:38 -07:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-04 04:23:10 -06:00
|
|
|
else if (input->count(interface_key::CUSTOM_P) ||
|
|
|
|
input->count(interface_key::CUSTOM_F) ||
|
|
|
|
input->count(interface_key::CUSTOM_Q) ||
|
|
|
|
input->count(interface_key::CUSTOM_D) ||
|
|
|
|
input->count(interface_key::CUSTOM_N))
|
|
|
|
{
|
|
|
|
show_help = true;
|
|
|
|
}
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2013-01-07 01:17:38 -07:00
|
|
|
if (is_planmode_enabled(type))
|
|
|
|
{
|
2013-02-08 20:49:36 -07:00
|
|
|
if (planner.inQuickFortMode() && planner.in_dummmy_screen)
|
2013-02-07 04:04:52 -07:00
|
|
|
{
|
|
|
|
if (input->count(interface_key::SELECT) || input->count(interface_key::SEC_SELECT)
|
|
|
|
|| input->count(interface_key::LEAVESCREEN))
|
|
|
|
{
|
|
|
|
planner.in_dummmy_screen = false;
|
|
|
|
send_key(interface_key::LEAVESCREEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-07 01:17:38 -07:00
|
|
|
if (input->count(interface_key::SELECT))
|
|
|
|
{
|
|
|
|
if (ui_build_selector->errors.size() == 0 && planner.allocatePlannedBuilding(type))
|
|
|
|
{
|
|
|
|
send_key(interface_key::CURSOR_DOWN_Z);
|
|
|
|
send_key(interface_key::CURSOR_UP_Z);
|
2013-02-08 20:49:36 -07:00
|
|
|
if (planner.inQuickFortMode())
|
2013-02-07 04:04:52 -07:00
|
|
|
{
|
|
|
|
planner.in_dummmy_screen = true;
|
|
|
|
}
|
2013-01-07 01:17:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-04 04:23:10 -06:00
|
|
|
else if (input->count(interface_key::CUSTOM_SHIFT_F))
|
2013-02-07 02:57:07 -07:00
|
|
|
{
|
2013-02-08 20:49:36 -07:00
|
|
|
if (!planner.inQuickFortMode())
|
|
|
|
{
|
|
|
|
planner.enableQuickfortMode();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
planner.disableQuickfortMode();
|
|
|
|
}
|
2013-02-07 02:57:07 -07:00
|
|
|
}
|
2014-05-04 04:23:10 -06:00
|
|
|
else if (input->count(interface_key::CUSTOM_SHIFT_M))
|
2013-01-18 23:20:37 -07:00
|
|
|
{
|
|
|
|
Screen::show(new ViewscreenChooseMaterial(planner.getDefaultItemFilterForType(type)));
|
|
|
|
}
|
2014-05-04 04:23:10 -06:00
|
|
|
else if (input->count(interface_key::CUSTOM_SHIFT_Q))
|
2013-01-18 23:20:37 -07:00
|
|
|
{
|
|
|
|
planner.cycleDefaultQuality(type);
|
|
|
|
}
|
2014-05-04 04:23:10 -06:00
|
|
|
else if (input->count(interface_key::CUSTOM_SHIFT_D))
|
2013-01-18 23:20:37 -07:00
|
|
|
{
|
|
|
|
planner.getDefaultItemFilterForType(type)->decorated_only =
|
|
|
|
!planner.getDefaultItemFilterForType(type)->decorated_only;
|
|
|
|
}
|
2013-01-07 01:17:38 -07:00
|
|
|
}
|
|
|
|
}
|
2013-01-18 23:20:37 -07:00
|
|
|
else if (isInPlannedBuildingQueryMode())
|
2013-01-07 01:17:38 -07:00
|
|
|
{
|
2013-01-18 23:20:37 -07:00
|
|
|
if (input->count(interface_key::SUSPENDBUILDING))
|
|
|
|
{
|
|
|
|
return true; // Don't unsuspend planned buildings
|
|
|
|
}
|
|
|
|
else if (input->count(interface_key::DESTROYBUILDING))
|
|
|
|
{
|
|
|
|
planner.removeSelectedPlannedBuilding(); // Remove persistent data
|
|
|
|
}
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2013-01-07 01:17:38 -07:00
|
|
|
}
|
2014-06-14 05:50:47 -06:00
|
|
|
else if (isInNobleRoomQueryMode())
|
|
|
|
{
|
|
|
|
auto np = getNoblePositionOfSelectedBuildingOwner();
|
2015-07-28 16:34:29 -06:00
|
|
|
df::interface_key last_token = get_string_key(input);
|
2014-06-14 05:50:47 -06:00
|
|
|
if (last_token >= interface_key::STRING_A048 && last_token <= interface_key::STRING_A058)
|
|
|
|
{
|
|
|
|
int selection = last_token - interface_key::STRING_A048;
|
|
|
|
if (np.size() < selection)
|
|
|
|
return false;
|
|
|
|
roomMonitor.toggleRoomForPosition(world->selected_building->id, np.at(selection-1).position->code);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-01-07 01:17:38 -07:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_VMETHOD_INTERPOSE(void, feed, (set<df::interface_key> *input))
|
|
|
|
{
|
2013-01-18 23:20:37 -07:00
|
|
|
if (!handleInput(input))
|
2013-01-07 01:17:38 -07:00
|
|
|
INTERPOSE_NEXT(feed)(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_VMETHOD_INTERPOSE(void, render, ())
|
|
|
|
{
|
|
|
|
bool plannable = isInPlannedBuildingPlacementMode();
|
|
|
|
if (plannable && is_planmode_enabled(ui_build_selector->building_type))
|
|
|
|
{
|
|
|
|
if (ui_build_selector->stage < 1)
|
|
|
|
{
|
|
|
|
// No materials but turn on cursor
|
|
|
|
ui_build_selector->stage = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto iter = ui_build_selector->errors.begin(); iter != ui_build_selector->errors.end();)
|
|
|
|
{
|
|
|
|
//FIXME Hide bags
|
|
|
|
if (((*iter)->find("Needs") != string::npos && **iter != "Needs adjacent wall") ||
|
|
|
|
(*iter)->find("No access") != string::npos)
|
|
|
|
{
|
|
|
|
iter = ui_build_selector->errors.erase(iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERPOSE_NEXT(render)();
|
|
|
|
|
|
|
|
auto dims = Gui::getDwarfmodeViewDims();
|
|
|
|
int left_margin = dims.menu_x1 + 1;
|
|
|
|
int x = left_margin;
|
2013-01-18 23:20:37 -07:00
|
|
|
auto type = ui_build_selector->building_type;
|
2013-01-07 01:17:38 -07:00
|
|
|
if (plannable)
|
|
|
|
{
|
2013-02-08 20:49:36 -07:00
|
|
|
if (planner.inQuickFortMode() && planner.in_dummmy_screen)
|
2013-02-07 04:04:52 -07:00
|
|
|
{
|
|
|
|
Screen::Pen pen(' ',COLOR_BLACK);
|
|
|
|
int y = dims.y1 + 1;
|
|
|
|
Screen::fillRect(pen, x, y, dims.menu_x2, y + 20);
|
2013-01-07 01:17:38 -07:00
|
|
|
|
2013-02-07 04:04:52 -07:00
|
|
|
++y;
|
2013-01-07 01:17:38 -07:00
|
|
|
|
2013-02-07 04:04:52 -07:00
|
|
|
OutputString(COLOR_BROWN, x, y, "Quickfort Placeholder", true, left_margin);
|
|
|
|
OutputString(COLOR_WHITE, x, y, "Enter, Shift-Enter or Esc", true, left_margin);
|
|
|
|
}
|
|
|
|
else
|
2013-01-07 01:17:38 -07:00
|
|
|
{
|
2013-02-07 04:04:52 -07:00
|
|
|
int y = 23;
|
2013-02-07 02:57:07 -07:00
|
|
|
|
2014-05-04 04:23:10 -06:00
|
|
|
if (show_help)
|
|
|
|
{
|
|
|
|
OutputString(COLOR_BROWN, x, y, "Note: ");
|
|
|
|
OutputString(COLOR_WHITE, x, y, "Use Shift-Keys here", true, left_margin);
|
|
|
|
}
|
|
|
|
OutputToggleString(x, y, "Planning Mode", "P", is_planmode_enabled(type), true, left_margin);
|
2013-01-18 23:20:37 -07:00
|
|
|
|
2013-02-07 04:04:52 -07:00
|
|
|
if (is_planmode_enabled(type))
|
|
|
|
{
|
2014-05-04 04:23:10 -06:00
|
|
|
OutputToggleString(x, y, "Quickfort Mode", "F", planner.inQuickFortMode(), true, left_margin);
|
2013-01-18 23:20:37 -07:00
|
|
|
|
2013-02-07 04:04:52 -07:00
|
|
|
auto filter = planner.getDefaultItemFilterForType(type);
|
|
|
|
|
2014-05-04 04:23:10 -06:00
|
|
|
OutputHotkeyString(x, y, "Min Quality: ", "Q");
|
2013-02-07 04:04:52 -07:00
|
|
|
OutputString(COLOR_BROWN, x, y, filter->getMinQuality(), true, left_margin);
|
|
|
|
|
2014-05-04 04:23:10 -06:00
|
|
|
OutputToggleString(x, y, "Decorated Only: ", "D", filter->decorated_only, true, left_margin);
|
2013-02-07 04:04:52 -07:00
|
|
|
|
2014-05-04 04:23:10 -06:00
|
|
|
OutputHotkeyString(x, y, "Material Filter:", "M", true, left_margin);
|
2013-02-07 04:04:52 -07:00
|
|
|
auto filter_descriptions = filter->getMaterialFilterAsVector();
|
2013-10-02 09:55:48 -06:00
|
|
|
for (auto it = filter_descriptions.begin(); it != filter_descriptions.end(); ++it)
|
|
|
|
OutputString(COLOR_BROWN, x, y, " *" + *it, true, left_margin);
|
2013-02-07 04:04:52 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
planner.in_dummmy_screen = false;
|
|
|
|
}
|
2013-01-07 01:17:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (isInPlannedBuildingQueryMode())
|
|
|
|
{
|
2013-02-07 04:04:52 -07:00
|
|
|
planner.in_dummmy_screen = false;
|
|
|
|
|
2013-01-07 01:17:38 -07:00
|
|
|
// Hide suspend toggle option
|
|
|
|
int y = 20;
|
|
|
|
Screen::Pen pen(' ', COLOR_BLACK);
|
|
|
|
Screen::fillRect(pen, x, y, dims.menu_x2, y);
|
2013-01-18 23:20:37 -07:00
|
|
|
|
|
|
|
auto filter = planner.getSelectedPlannedBuilding()->getFilter();
|
|
|
|
y = 24;
|
|
|
|
OutputString(COLOR_BROWN, x, y, "Planned Building Filter:", true, left_margin);
|
|
|
|
OutputString(COLOR_BLUE, x, y, filter->getMinQuality(), true, left_margin);
|
|
|
|
|
|
|
|
if (filter->decorated_only)
|
|
|
|
OutputString(COLOR_BLUE, x, y, "Decorated Only", true, left_margin);
|
2013-01-29 00:44:56 -07:00
|
|
|
|
2013-01-18 23:20:37 -07:00
|
|
|
OutputString(COLOR_BROWN, x, y, "Materials:", true, left_margin);
|
2013-01-29 00:44:56 -07:00
|
|
|
auto filters = filter->getMaterialFilterAsVector();
|
2013-10-02 09:55:48 -06:00
|
|
|
for (auto it = filters.begin(); it != filters.end(); ++it)
|
|
|
|
OutputString(COLOR_BLUE, x, y, "*" + *it, true, left_margin);
|
2013-01-07 01:17:38 -07:00
|
|
|
}
|
2014-06-14 05:50:47 -06:00
|
|
|
else if (isInNobleRoomQueryMode())
|
|
|
|
{
|
|
|
|
auto np = getNoblePositionOfSelectedBuildingOwner();
|
|
|
|
int y = 24;
|
|
|
|
OutputString(COLOR_BROWN, x, y, "DFHack", true, left_margin);
|
|
|
|
OutputString(COLOR_WHITE, x, y, "Auto-allocate to:", true, left_margin);
|
|
|
|
for (int i = 0; i < np.size() && i < 9; i++)
|
|
|
|
{
|
|
|
|
bool enabled = (roomMonitor.getReservedNobleCode(world->selected_building->id)
|
|
|
|
== np[i].position->code);
|
|
|
|
OutputToggleString(x, y, np[i].position->name[0].c_str(),
|
|
|
|
int_to_string(i+1).c_str(), enabled, true, left_margin);
|
|
|
|
}
|
|
|
|
}
|
2013-02-07 04:04:52 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
planner.in_dummmy_screen = false;
|
2014-05-04 04:23:10 -06:00
|
|
|
show_help = false;
|
2013-02-07 04:04:52 -07:00
|
|
|
}
|
2013-01-07 01:17:38 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
IMPLEMENT_VMETHOD_INTERPOSE(buildingplan_hook, feed);
|
|
|
|
IMPLEMENT_VMETHOD_INTERPOSE(buildingplan_hook, render);
|
|
|
|
|
|
|
|
|
|
|
|
static command_result buildingplan_cmd(color_ostream &out, vector <string> & parameters)
|
|
|
|
{
|
|
|
|
if (!parameters.empty())
|
|
|
|
{
|
2013-03-09 20:14:00 -07:00
|
|
|
if (parameters.size() == 1 && toLower(parameters[0])[0] == 'v')
|
|
|
|
{
|
|
|
|
out << "Building Plan" << endl << "Version: " << PLUGIN_VERSION << endl;
|
|
|
|
}
|
|
|
|
else if (parameters.size() == 2 && toLower(parameters[0]) == "debug")
|
|
|
|
{
|
|
|
|
show_debugging = (toLower(parameters[1]) == "on");
|
|
|
|
out << "Debugging " << ((show_debugging) ? "enabled" : "disabled") << endl;
|
2015-02-14 20:53:06 -07:00
|
|
|
}
|
2013-01-07 01:17:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-30 03:19:51 -06:00
|
|
|
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
|
2013-01-07 01:17:38 -07:00
|
|
|
|
2013-09-30 03:19:51 -06:00
|
|
|
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable)
|
2013-01-07 01:17:38 -07:00
|
|
|
{
|
2013-09-30 03:19:51 -06:00
|
|
|
if (!gps)
|
|
|
|
return CR_FAILURE;
|
|
|
|
|
|
|
|
if (enable != is_enabled)
|
|
|
|
{
|
|
|
|
planner.reset(out);
|
|
|
|
|
|
|
|
if (!INTERPOSE_HOOK(buildingplan_hook, feed).apply(enable) ||
|
|
|
|
!INTERPOSE_HOOK(buildingplan_hook, render).apply(enable))
|
|
|
|
return CR_FAILURE;
|
|
|
|
|
|
|
|
is_enabled = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
2013-01-07 01:17:38 -07:00
|
|
|
|
2013-09-30 03:19:51 -06:00
|
|
|
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
|
|
|
|
{
|
2013-01-07 01:17:38 -07:00
|
|
|
commands.push_back(
|
|
|
|
PluginCommand(
|
|
|
|
"buildingplan", "Place furniture before it's built",
|
2013-10-30 14:58:14 -06:00
|
|
|
buildingplan_cmd, false, "Run 'buildingplan debug [on|off]' to toggle debugging, or 'buildingplan version' to query the plugin version."));
|
2013-01-07 01:17:38 -07:00
|
|
|
planner.initialize();
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
|
|
|
|
{
|
|
|
|
switch (event) {
|
|
|
|
case SC_MAP_LOADED:
|
2013-01-18 23:20:37 -07:00
|
|
|
planner.reset(out);
|
2014-06-14 05:50:47 -06:00
|
|
|
roomMonitor.reset(out);
|
2013-01-07 01:17:38 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CR_OK;
|
2013-01-29 00:44:56 -07:00
|
|
|
}
|