Code cleanup

develop
Anuradha Dissanayake 2012-11-21 23:45:26 +13:00
parent 0d9efec062
commit 2a83d92290
1 changed files with 45 additions and 79 deletions

@ -14,13 +14,14 @@
// DF data structure definition headers // DF data structure definition headers
#include "DataDefs.h" #include "DataDefs.h"
#include "MiscUtils.h" #include "MiscUtils.h"
#include "df/viewscreen_dwarfmodest.h"
#include "df/ui_build_selector.h"
#include "df/build_req_choice_genst.h" #include "df/build_req_choice_genst.h"
#include "df/build_req_choice_specst.h" #include "df/build_req_choice_specst.h"
#include "df/construction_type.h"
#include "df/item.h" #include "df/item.h"
#include "df/ui.h" #include "df/ui.h"
#include "df/ui_build_selector.h"
#include "df/viewscreen_dwarfmodest.h"
#include "modules/Gui.h" #include "modules/Gui.h"
#include "modules/Screen.h" #include "modules/Screen.h"
@ -34,7 +35,6 @@ using df::global::gps;
using df::global::ui; using df::global::ui;
using df::global::ui_build_selector; using df::global::ui_build_selector;
DFHACK_PLUGIN("automaterial"); DFHACK_PLUGIN("automaterial");
struct MaterialDescriptor struct MaterialDescriptor
@ -55,12 +55,10 @@ struct MaterialDescriptor
} }
}; };
typedef int16_t construction_type; static map<int16_t, MaterialDescriptor> last_used_material;
static map<int16_t, MaterialDescriptor> last_moved_material;
static map<construction_type, MaterialDescriptor> last_used_material; static map< int16_t, vector<MaterialDescriptor> > preferred_materials;
static map<construction_type, MaterialDescriptor> last_moved_material; static map< int16_t, df::interface_key > hotkeys;
static map< construction_type, vector<MaterialDescriptor> > preferred_materials;
static map< construction_type, df::interface_key > hotkeys;
static bool last_used_moved = false; static bool last_used_moved = false;
static bool auto_choose_materials = true; static bool auto_choose_materials = true;
static bool auto_choose_attempted = true; static bool auto_choose_attempted = true;
@ -71,7 +69,6 @@ static command_result automaterial_cmd(color_ostream &out, vector <string> & par
return CR_OK; return CR_OK;
} }
DFhackCExport command_result plugin_shutdown ( color_ostream &out ) DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{ {
return CR_OK; return CR_OK;
@ -183,7 +180,6 @@ static MaterialDescriptor get_material_in_list(size_t i)
return result; return result;
} }
static bool is_material_in_autoselect(size_t &i, MaterialDescriptor &material) static bool is_material_in_autoselect(size_t &i, MaterialDescriptor &material)
{ {
for (i = 0; i < get_curr_constr_prefs().size(); i++) for (i = 0; i < get_curr_constr_prefs().size(); i++)
@ -225,27 +221,6 @@ static bool move_material_to_top(MaterialDescriptor &material)
return false; return false;
} }
static bool choose_materials()
{
size_t size = ui_build_selector->choices.size();
for (size_t i = 0; i < size; i++)
{
MaterialDescriptor material = get_material_in_list(i);
size_t j;
if (is_material_in_autoselect(j, material))
{
ui_build_selector->sel_index = i;
std::set< df::interface_key > keys;
keys.insert(df::interface_key::SELECT_ALL);
Core::getTopViewscreen()->feed(&keys);
if (!in_material_choice_stage())
return true;
}
}
return false;
}
static bool check_autoselect(MaterialDescriptor &material, bool toggle) static bool check_autoselect(MaterialDescriptor &material, bool toggle)
{ {
size_t idx; size_t idx;
@ -269,6 +244,27 @@ struct jobutils_hook : public df::viewscreen_dwarfmodest
{ {
typedef df::viewscreen_dwarfmodest interpose_base; typedef df::viewscreen_dwarfmodest interpose_base;
bool choose_materials()
{
size_t size = ui_build_selector->choices.size();
for (size_t i = 0; i < size; i++)
{
MaterialDescriptor material = get_material_in_list(i);
size_t j;
if (is_material_in_autoselect(j, material))
{
ui_build_selector->sel_index = i;
std::set< df::interface_key > keys;
keys.insert(df::interface_key::SELECT_ALL);
this->feed(&keys);
if (!in_material_choice_stage())
return true;
}
}
return false;
}
DEFINE_VMETHOD_INTERPOSE(void, feed, (set<df::interface_key> *input)) DEFINE_VMETHOD_INTERPOSE(void, feed, (set<df::interface_key> *input))
{ {
if (in_material_choice_stage()) if (in_material_choice_stage())
@ -302,15 +298,17 @@ struct jobutils_hook : public df::viewscreen_dwarfmodest
} }
} }
construction_type last_used_constr_subtype = (in_material_choice_stage()) ? ui_build_selector->building_subtype : -1; int16_t last_used_constr_subtype = (in_material_choice_stage()) ? ui_build_selector->building_subtype : -1;
INTERPOSE_NEXT(feed)(input); INTERPOSE_NEXT(feed)(input);
if (revert_to_last_used_type && last_used_constr_subtype >= 0 && !in_material_choice_stage()) if (revert_to_last_used_type &&
last_used_constr_subtype >= 0 &&
!in_material_choice_stage() &&
hotkeys.find(last_used_constr_subtype) != hotkeys.end())
{ {
input->clear(); input->clear();
std::set< df::interface_key > keys; input->insert(hotkeys[last_used_constr_subtype]);
keys.insert(hotkeys[last_used_constr_subtype]); this->feed(input);
Core::getTopViewscreen()->feed(&keys);
} }
} }
@ -394,46 +392,14 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <Plug
if (!gps || !INTERPOSE_HOOK(jobutils_hook, feed).apply() || !INTERPOSE_HOOK(jobutils_hook, render).apply()) if (!gps || !INTERPOSE_HOOK(jobutils_hook, feed).apply() || !INTERPOSE_HOOK(jobutils_hook, render).apply())
out.printerr("Could not insert jobutils hooks!\n"); out.printerr("Could not insert jobutils hooks!\n");
hotkeys[1] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_WALL; hotkeys[construction_type::Wall] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_WALL;
hotkeys[2] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_FLOOR; hotkeys[construction_type::Floor] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_FLOOR;
hotkeys[6] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_RAMP; hotkeys[construction_type::Ramp] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_RAMP;
hotkeys[3] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_STAIR_UP; hotkeys[construction_type::UpStair] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_STAIR_UP;
hotkeys[4] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_STAIR_DOWN; hotkeys[construction_type::DownStair] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_STAIR_DOWN;
hotkeys[5] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_STAIR_UPDOWN; hotkeys[construction_type::UpDownStair] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_STAIR_UPDOWN;
hotkeys[0] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_FORTIFICATION; hotkeys[construction_type::Fortification] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_FORTIFICATION;
hotkeys[7] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_TRACK; //Ignore tracks, DF already returns to track menu
hotkeys[5] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_TRACK_STOP;
commands.push_back(PluginCommand(
"automaterial", "Makes construction easier by auto selecting materials",
automaterial_cmd, false, /* true means that the command can't be used from non-interactive user interface */
// Extended help string. Used by CR_WRONG_USAGE and the help command:
" Makes construction easier by auto selecting materials.\n"
));
return CR_OK;
}
/*
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
{
switch (event) {
case SC_GAME_LOADED:
// initialize from the world just loaded
break;
case SC_GAME_UNLOADED:
// cleanup
break;
default:
break;
}
return CR_OK; return CR_OK;
} }
*/
/*
DFhackCExport command_result plugin_onupdate ( color_ostream &out )
{
return CR_OK;
}
*/