Merge branch 'master' of https://github.com/angavrilov/dfhack.git
Conflicts: scripts/autofarm.rb (resolved with added bug correction)develop
Before Width: | Height: | Size: 7.9 KiB After Width: | Height: | Size: 3.8 KiB |
After Width: | Height: | Size: 5.1 KiB |
After Width: | Height: | Size: 2.9 KiB |
Before Width: | Height: | Size: 8.0 KiB After Width: | Height: | Size: 4.8 KiB |
Before Width: | Height: | Size: 6.3 KiB After Width: | Height: | Size: 3.6 KiB |
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 7.5 KiB |
After Width: | Height: | Size: 7.4 KiB |
Before Width: | Height: | Size: 6.8 KiB After Width: | Height: | Size: 3.7 KiB |
Before Width: | Height: | Size: 7.3 KiB After Width: | Height: | Size: 4.3 KiB |
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 6.5 KiB |
Before Width: | Height: | Size: 9.9 KiB After Width: | Height: | Size: 6.0 KiB |
Before Width: | Height: | Size: 8.4 KiB After Width: | Height: | Size: 4.7 KiB |
Before Width: | Height: | Size: 7.1 KiB After Width: | Height: | Size: 4.4 KiB |
Before Width: | Height: | Size: 8.0 KiB After Width: | Height: | Size: 3.9 KiB |
After Width: | Height: | Size: 6.8 KiB |
After Width: | Height: | Size: 3.3 KiB |
Before Width: | Height: | Size: 9.2 KiB After Width: | Height: | Size: 6.5 KiB |
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 6.7 KiB |
Before Width: | Height: | Size: 8.0 KiB After Width: | Height: | Size: 4.8 KiB |
Before Width: | Height: | Size: 8.1 KiB After Width: | Height: | Size: 4.7 KiB |
Before Width: | Height: | Size: 9.1 KiB After Width: | Height: | Size: 5.4 KiB |
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 6.3 KiB |
@ -0,0 +1,378 @@
|
||||
// Auto Material Select
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "Core.h"
|
||||
#include <Console.h>
|
||||
#include <Export.h>
|
||||
#include <PluginManager.h>
|
||||
#include <VTableInterpose.h>
|
||||
|
||||
|
||||
// DF data structure definition headers
|
||||
#include "DataDefs.h"
|
||||
#include "MiscUtils.h"
|
||||
#include "df/build_req_choice_genst.h"
|
||||
#include "df/build_req_choice_specst.h"
|
||||
#include "df/construction_type.h"
|
||||
#include "df/item.h"
|
||||
#include "df/ui.h"
|
||||
#include "df/ui_build_selector.h"
|
||||
#include "df/viewscreen_dwarfmodest.h"
|
||||
|
||||
#include "modules/Gui.h"
|
||||
#include "modules/Screen.h"
|
||||
|
||||
using std::map;
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
using namespace DFHack;
|
||||
using namespace df::enums;
|
||||
using df::global::gps;
|
||||
using df::global::ui;
|
||||
using df::global::ui_build_selector;
|
||||
|
||||
DFHACK_PLUGIN("automaterial");
|
||||
|
||||
struct MaterialDescriptor
|
||||
{
|
||||
df::item_type item_type;
|
||||
int16_t item_subtype;
|
||||
int16_t type;
|
||||
int32_t index;
|
||||
bool valid;
|
||||
|
||||
bool matches(const MaterialDescriptor &a) const
|
||||
{
|
||||
return a.valid && valid &&
|
||||
a.type == type &&
|
||||
a.index == index &&
|
||||
a.item_type == item_type &&
|
||||
a.item_subtype == item_subtype;
|
||||
}
|
||||
};
|
||||
|
||||
static map<int16_t, MaterialDescriptor> last_used_material;
|
||||
static map<int16_t, MaterialDescriptor> last_moved_material;
|
||||
static map< int16_t, vector<MaterialDescriptor> > preferred_materials;
|
||||
static map< int16_t, df::interface_key > hotkeys;
|
||||
static bool last_used_moved = false;
|
||||
static bool auto_choose_materials = true;
|
||||
static bool auto_choose_attempted = true;
|
||||
static bool revert_to_last_used_type = false;
|
||||
|
||||
static command_result automaterial_cmd(color_ostream &out, vector <string> & parameters)
|
||||
{
|
||||
return CR_OK;
|
||||
}
|
||||
|
||||
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
|
||||
{
|
||||
return CR_OK;
|
||||
}
|
||||
|
||||
static inline bool in_material_choice_stage()
|
||||
{
|
||||
return Gui::build_selector_hotkey(Core::getTopViewscreen()) &&
|
||||
ui_build_selector->building_type == df::building_type::Construction &&
|
||||
ui->main.mode == ui_sidebar_mode::Build &&
|
||||
ui_build_selector->stage == 2;
|
||||
}
|
||||
|
||||
static inline bool in_placement_stage()
|
||||
{
|
||||
return Gui::dwarfmode_hotkey(Core::getTopViewscreen()) &&
|
||||
ui->main.mode == ui_sidebar_mode::Build &&
|
||||
ui_build_selector &&
|
||||
ui_build_selector->building_type == df::building_type::Construction &&
|
||||
ui_build_selector->stage == 1;
|
||||
}
|
||||
|
||||
static inline bool in_type_choice_stage()
|
||||
{
|
||||
return Gui::dwarfmode_hotkey(Core::getTopViewscreen()) &&
|
||||
ui->main.mode == ui_sidebar_mode::Build &&
|
||||
ui_build_selector &&
|
||||
ui_build_selector->building_type < 0;
|
||||
}
|
||||
|
||||
static inline vector<MaterialDescriptor> &get_curr_constr_prefs()
|
||||
{
|
||||
if (preferred_materials.find(ui_build_selector->building_subtype) == preferred_materials.end())
|
||||
preferred_materials[ui_build_selector->building_subtype] = vector<MaterialDescriptor>();
|
||||
|
||||
return preferred_materials[ui_build_selector->building_subtype];
|
||||
}
|
||||
|
||||
static inline MaterialDescriptor &get_last_used_material()
|
||||
{
|
||||
if (last_used_material.find(ui_build_selector->building_subtype) == last_used_material.end())
|
||||
last_used_material[ui_build_selector->building_subtype] = MaterialDescriptor();
|
||||
|
||||
return last_used_material[ui_build_selector->building_subtype];
|
||||
}
|
||||
|
||||
static void set_last_used_material(const MaterialDescriptor &matetial)
|
||||
{
|
||||
last_used_material[ui_build_selector->building_subtype] = matetial;
|
||||
}
|
||||
|
||||
static MaterialDescriptor &get_last_moved_material()
|
||||
{
|
||||
if (last_moved_material.find(ui_build_selector->building_subtype) == last_moved_material.end())
|
||||
last_moved_material[ui_build_selector->building_subtype] = MaterialDescriptor();
|
||||
|
||||
return last_moved_material[ui_build_selector->building_subtype];
|
||||
}
|
||||
|
||||
static void set_last_moved_material(const MaterialDescriptor &matetial)
|
||||
{
|
||||
last_moved_material[ui_build_selector->building_subtype] = matetial;
|
||||
}
|
||||
|
||||
static MaterialDescriptor get_material_in_list(size_t i)
|
||||
{
|
||||
MaterialDescriptor result;
|
||||
result.valid = false;
|
||||
|
||||
if (VIRTUAL_CAST_VAR(gen, df::build_req_choice_genst, ui_build_selector->choices[i]))
|
||||
{
|
||||
result.item_type = gen->item_type;
|
||||
result.item_subtype = gen->item_subtype;
|
||||
result.type = gen->mat_type;
|
||||
result.index = gen->mat_index;
|
||||
result.valid = true;
|
||||
}
|
||||
else if (VIRTUAL_CAST_VAR(spec, df::build_req_choice_specst, ui_build_selector->choices[i]))
|
||||
{
|
||||
result.item_type = gen->item_type;
|
||||
result.item_subtype = gen->item_subtype;
|
||||
result.type = spec->candidate->getActualMaterial();
|
||||
result.index = spec->candidate->getActualMaterialIndex();
|
||||
result.valid = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool is_material_in_autoselect(size_t &i, MaterialDescriptor &material)
|
||||
{
|
||||
for (i = 0; i < get_curr_constr_prefs().size(); i++)
|
||||
{
|
||||
if (get_curr_constr_prefs()[i].matches(material))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool is_material_in_list(size_t &i, MaterialDescriptor &material)
|
||||
{
|
||||
const size_t size = ui_build_selector->choices.size(); //Just because material list could be very big
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
if (get_material_in_list(i).matches(material))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool move_material_to_top(MaterialDescriptor &material)
|
||||
{
|
||||
size_t i;
|
||||
if (is_material_in_list(i, material))
|
||||
{
|
||||
auto sel_item = ui_build_selector->choices[i];
|
||||
ui_build_selector->choices.erase(ui_build_selector->choices.begin() + i);
|
||||
ui_build_selector->choices.insert(ui_build_selector->choices.begin(), sel_item);
|
||||
|
||||
ui_build_selector->sel_index = 0;
|
||||
set_last_moved_material(material);
|
||||
return true;
|
||||
}
|
||||
|
||||
set_last_moved_material(MaterialDescriptor());
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool check_autoselect(MaterialDescriptor &material, bool toggle)
|
||||
{
|
||||
size_t idx;
|
||||
if (is_material_in_autoselect(idx, material))
|
||||
{
|
||||
if (toggle)
|
||||
vector_erase_at(get_curr_constr_prefs(), idx);
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (toggle)
|
||||
get_curr_constr_prefs().push_back(material);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
struct jobutils_hook : public df::viewscreen_dwarfmodest
|
||||
{
|
||||
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))
|
||||
{
|
||||
if (in_material_choice_stage())
|
||||
{
|
||||
MaterialDescriptor material = get_material_in_list(ui_build_selector->sel_index);
|
||||
if (material.valid)
|
||||
{
|
||||
if (input->count(interface_key::SELECT) || input->count(interface_key::SEC_SELECT))
|
||||
{
|
||||
if (get_last_moved_material().matches(material))
|
||||
last_used_moved = false;
|
||||
|
||||
set_last_used_material(material);
|
||||
}
|
||||
else if (input->count(interface_key::CUSTOM_A))
|
||||
{
|
||||
check_autoselect(material, true);
|
||||
input->clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (in_placement_stage())
|
||||
{
|
||||
if (input->count(interface_key::CUSTOM_A))
|
||||
{
|
||||
auto_choose_materials = !auto_choose_materials;
|
||||
}
|
||||
else if (input->count(interface_key::CUSTOM_T))
|
||||
{
|
||||
revert_to_last_used_type = !revert_to_last_used_type;
|
||||
}
|
||||
}
|
||||
|
||||
int16_t last_used_constr_subtype = (in_material_choice_stage()) ? ui_build_selector->building_subtype : -1;
|
||||
INTERPOSE_NEXT(feed)(input);
|
||||
|
||||
if (revert_to_last_used_type &&
|
||||
last_used_constr_subtype >= 0 &&
|
||||
!in_material_choice_stage() &&
|
||||
hotkeys.find(last_used_constr_subtype) != hotkeys.end())
|
||||
{
|
||||
interface_key_set keys;
|
||||
keys.insert(hotkeys[last_used_constr_subtype]);
|
||||
INTERPOSE_NEXT(feed)(&keys);
|
||||
}
|
||||
}
|
||||
|
||||
DEFINE_VMETHOD_INTERPOSE(void, render, ())
|
||||
{
|
||||
if (in_material_choice_stage())
|
||||
{
|
||||
if (!last_used_moved)
|
||||
{
|
||||
if (auto_choose_materials && get_curr_constr_prefs().size() > 0)
|
||||
{
|
||||
last_used_moved = true;
|
||||
if (choose_materials())
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (ui_build_selector->is_grouped)
|
||||
{
|
||||
last_used_moved = true;
|
||||
move_material_to_top(get_last_used_material());
|
||||
}
|
||||
}
|
||||
else if (!ui_build_selector->is_grouped)
|
||||
{
|
||||
last_used_moved = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
last_used_moved = false;
|
||||
}
|
||||
|
||||
INTERPOSE_NEXT(render)();
|
||||
|
||||
if (in_material_choice_stage())
|
||||
{
|
||||
MaterialDescriptor material = get_material_in_list(ui_build_selector->sel_index);
|
||||
if (material.valid)
|
||||
{
|
||||
string title = "Disabled";
|
||||
if (check_autoselect(material, false))
|
||||
{
|
||||
title = "Enabled";
|
||||
}
|
||||
|
||||
auto dims = Gui::getDwarfmodeViewDims();
|
||||
Screen::Painter dc(dims.menu());
|
||||
|
||||
dc.seek(1,24).key_pen(COLOR_LIGHTRED).pen(COLOR_WHITE);
|
||||
dc.key(interface_key::CUSTOM_A).string(": Autoselect "+title);
|
||||
}
|
||||
}
|
||||
else if (in_placement_stage() && ui_build_selector->building_subtype < construction_type::TrackN)
|
||||
{
|
||||
string autoselect_toggle = (auto_choose_materials) ? "Disable" : "Enable";
|
||||
string revert_toggle = (revert_to_last_used_type) ? "Disable" : "Enable";
|
||||
|
||||
auto dims = Gui::getDwarfmodeViewDims();
|
||||
Screen::Painter dc(dims.menu());
|
||||
|
||||
dc.seek(1,23).key_pen(COLOR_LIGHTRED).pen(COLOR_WHITE);
|
||||
dc.key(interface_key::CUSTOM_A).string(": "+autoselect_toggle+" Auto Mat-Select").newline(1);
|
||||
dc.key(interface_key::CUSTOM_T).string(": "+revert_toggle+" Auto Type-Select");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
IMPLEMENT_VMETHOD_INTERPOSE(jobutils_hook, feed);
|
||||
IMPLEMENT_VMETHOD_INTERPOSE(jobutils_hook, render);
|
||||
|
||||
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
|
||||
{
|
||||
if (!gps || !ui_build_selector ||
|
||||
!INTERPOSE_HOOK(jobutils_hook, feed).apply() ||
|
||||
!INTERPOSE_HOOK(jobutils_hook, render).apply())
|
||||
out.printerr("Could not insert jobutils hooks!\n");
|
||||
|
||||
hotkeys[construction_type::Wall] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_WALL;
|
||||
hotkeys[construction_type::Floor] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_FLOOR;
|
||||
hotkeys[construction_type::Ramp] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_RAMP;
|
||||
hotkeys[construction_type::UpStair] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_STAIR_UP;
|
||||
hotkeys[construction_type::DownStair] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_STAIR_DOWN;
|
||||
hotkeys[construction_type::UpDownStair] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_STAIR_UPDOWN;
|
||||
hotkeys[construction_type::Fortification] = df::interface_key::HOTKEY_BUILDING_CONSTRUCTION_FORTIFICATION;
|
||||
//Ignore tracks, DF already returns to track menu
|
||||
|
||||
return CR_OK;
|
||||
}
|
@ -0,0 +1,120 @@
|
||||
# control your levers from the dfhack console
|
||||
|
||||
def lever_pull_job(bld)
|
||||
ref = DFHack::GeneralRefBuildingHolderst.cpp_new
|
||||
ref.building_id = bld.id
|
||||
|
||||
job = DFHack::Job.cpp_new
|
||||
job.job_type = :PullLever
|
||||
job.pos = [bld.centerx, bld.centery, bld.z]
|
||||
job.general_refs << ref
|
||||
bld.jobs << job
|
||||
df.job_link job
|
||||
|
||||
puts lever_descr(bld)
|
||||
end
|
||||
|
||||
def lever_pull_cheat(bld)
|
||||
bld.linked_mechanisms.each { |i|
|
||||
i.general_refs.grep(DFHack::GeneralRefBuildingHolderst).each { |r|
|
||||
r.building_tg.setTriggerState(bld.state)
|
||||
}
|
||||
}
|
||||
|
||||
bld.state = (bld.state == 0 ? 1 : 0)
|
||||
|
||||
puts lever_descr(bld)
|
||||
end
|
||||
|
||||
def lever_descr(bld, idx=nil)
|
||||
ret = []
|
||||
|
||||
# lever description
|
||||
descr = ''
|
||||
descr << "#{idx}: " if idx
|
||||
descr << "lever ##{bld.id} @[#{bld.centerx}, #{bld.centery}, #{bld.z}] #{bld.state == 0 ? '\\' : '/'}"
|
||||
bld.jobs.each { |j|
|
||||
if j.job_type == :PullLever
|
||||
flags = ''
|
||||
flags << ', repeat' if j.flags.repeat
|
||||
flags << ', suspended' if j.flags.suspend
|
||||
descr << " (pull order#{flags})"
|
||||
end
|
||||
}
|
||||
|
||||
bld.linked_mechanisms.map { |i|
|
||||
i.general_refs.grep(DFHack::GeneralRefBuildingHolderst)
|
||||
}.flatten.each { |r|
|
||||
# linked building description
|
||||
tg = r.building_tg
|
||||
state = ''
|
||||
if tg.respond_to?(:gate_flags)
|
||||
state << (tg.gate_flags.closed ? 'closed' : 'opened')
|
||||
state << ", closing (#{tg.timer})" if tg.gate_flags.closing
|
||||
state << ", opening (#{tg.timer})" if tg.gate_flags.opening
|
||||
end
|
||||
|
||||
ret << (descr + " linked to #{tg._rtti_classname} ##{tg.id} @[#{tg.centerx}, #{tg.centery}, #{tg.z}] #{state}")
|
||||
|
||||
# indent other links
|
||||
descr = descr.gsub(/./, ' ')
|
||||
}
|
||||
|
||||
ret << descr if ret.empty?
|
||||
|
||||
ret
|
||||
end
|
||||
|
||||
def lever_list
|
||||
@lever_list = []
|
||||
df.world.buildings.other[:TRAP].find_all { |bld|
|
||||
bld.trap_type == :Lever
|
||||
}.sort_by { |bld| bld.id }.each { |bld|
|
||||
puts lever_descr(bld, @lever_list.length)
|
||||
@lever_list << bld.id
|
||||
}
|
||||
end
|
||||
|
||||
case $script_args[0]
|
||||
when 'pull'
|
||||
cheat = $script_args.delete('--cheat') || $script_args.delete('--now')
|
||||
|
||||
id = $script_args[1].to_i
|
||||
id = @lever_list[id] || id
|
||||
bld = df.building_find(id)
|
||||
raise 'invalid lever id' if not bld
|
||||
|
||||
if cheat
|
||||
lever_pull_cheat(bld)
|
||||
else
|
||||
lever_pull_job(bld)
|
||||
end
|
||||
|
||||
when 'list'
|
||||
lever_list
|
||||
|
||||
when /^\d+$/
|
||||
id = $script_args[0].to_i
|
||||
id = @lever_list[id] || id
|
||||
bld = df.building_find(id)
|
||||
raise 'invalid lever id' if not bld
|
||||
|
||||
puts lever_descr(bld)
|
||||
|
||||
else
|
||||
puts <<EOS
|
||||
Lever control from the dfhack console
|
||||
|
||||
Usage:
|
||||
lever list
|
||||
shows the list of levers in the fortress, with their id and links
|
||||
|
||||
lever pull 42
|
||||
order the dwarves to pull lever 42
|
||||
|
||||
lever pull 42 --cheat
|
||||
magically pull lever 42 immediately
|
||||
EOS
|
||||
|
||||
end
|
||||
|
@ -0,0 +1,200 @@
|
||||
# mark stuff inside of cages for dumping.
|
||||
|
||||
def plural(nr, name)
|
||||
# '1 cage' / '4 cages'
|
||||
"#{nr} #{name}#{'s' if nr > 1}"
|
||||
end
|
||||
|
||||
def cage_dump_items(list)
|
||||
count = 0
|
||||
count_cage = 0
|
||||
list.each { |cage|
|
||||
pre_count = count
|
||||
cage.general_refs.each { |ref|
|
||||
next unless ref.kind_of?(DFHack::GeneralRefContainsItemst)
|
||||
next if ref.item_tg.flags.dump
|
||||
count += 1
|
||||
ref.item_tg.flags.dump = true
|
||||
}
|
||||
count_cage += 1 if pre_count != count
|
||||
}
|
||||
|
||||
puts "Dumped #{plural(count, 'item')} in #{plural(count_cage, 'cage')}"
|
||||
end
|
||||
|
||||
def cage_dump_armor(list)
|
||||
count = 0
|
||||
count_cage = 0
|
||||
list.each { |cage|
|
||||
pre_count = count
|
||||
cage.general_refs.each { |ref|
|
||||
next unless ref.kind_of?(DFHack::GeneralRefContainsUnitst)
|
||||
ref.unit_tg.inventory.each { |it|
|
||||
next if it.mode != :Worn
|
||||
next if it.item.flags.dump
|
||||
count += 1
|
||||
it.item.flags.dump = true
|
||||
}
|
||||
}
|
||||
count_cage += 1 if pre_count != count
|
||||
}
|
||||
|
||||
puts "Dumped #{plural(count, 'armor piece')} in #{plural(count_cage, 'cage')}"
|
||||
end
|
||||
|
||||
def cage_dump_weapons(list)
|
||||
count = 0
|
||||
count_cage = 0
|
||||
list.each { |cage|
|
||||
pre_count = count
|
||||
cage.general_refs.each { |ref|
|
||||
next unless ref.kind_of?(DFHack::GeneralRefContainsUnitst)
|
||||
ref.unit_tg.inventory.each { |it|
|
||||
next if it.mode != :Weapon
|
||||
next if it.item.flags.dump
|
||||
count += 1
|
||||
it.item.flags.dump = true
|
||||
}
|
||||
}
|
||||
count_cage += 1 if pre_count != count
|
||||
}
|
||||
|
||||
puts "Dumped #{plural(count, 'weapon')} in #{plural(count_cage, 'cage')}"
|
||||
end
|
||||
|
||||
def cage_dump_all(list)
|
||||
count = 0
|
||||
count_cage = 0
|
||||
list.each { |cage|
|
||||
pre_count = count
|
||||
cage.general_refs.each { |ref|
|
||||
case ref
|
||||
when DFHack::GeneralRefContainsItemst
|
||||
next if ref.item_tg.flags.dump
|
||||
count += 1
|
||||
ref.item_tg.flags.dump = true
|
||||
when DFHack::GeneralRefContainsUnitst
|
||||
ref.unit_tg.inventory.each { |it|
|
||||
next if it.item.flags.dump
|
||||
count += 1
|
||||
it.item.flags.dump = true
|
||||
}
|
||||
end
|
||||
}
|
||||
count_cage += 1 if pre_count != count
|
||||
}
|
||||
|
||||
puts "Dumped #{plural(count, 'item')} in #{plural(count_cage, 'cage')}"
|
||||
end
|
||||
|
||||
|
||||
def cage_dump_list(list)
|
||||
count_total = Hash.new(0)
|
||||
empty_cages = 0
|
||||
list.each { |cage|
|
||||
count = Hash.new(0)
|
||||
|
||||
cage.general_refs.each { |ref|
|
||||
case ref
|
||||
when DFHack::GeneralRefContainsItemst
|
||||
count[ref.item_tg._rtti_classname] += 1
|
||||
when DFHack::GeneralRefContainsUnitst
|
||||
ref.unit_tg.inventory.each { |it|
|
||||
count[it.item._rtti_classname] += 1
|
||||
}
|
||||
# TODO vermin ?
|
||||
else
|
||||
puts "unhandled ref #{ref.inspect}" if $DEBUG
|
||||
end
|
||||
}
|
||||
|
||||
type = case cage
|
||||
when DFHack::ItemCagest; 'Cage'
|
||||
when DFHack::ItemAnimaltrapst; 'Animal trap'
|
||||
else cage._rtti_classname
|
||||
end
|
||||
|
||||
if count.empty?
|
||||
empty_cages += 1
|
||||
else
|
||||
puts "#{type} ##{cage.id}: ", count.sort_by { |k, v| v }.map { |k, v| " #{v} #{k}" }
|
||||
end
|
||||
|
||||
count.each { |k, v| count_total[k] += v }
|
||||
}
|
||||
|
||||
if list.length > 2
|
||||
puts '', "Total: ", count_total.sort_by { |k, v| v }.map { |k, v| " #{v} #{k}" }
|
||||
puts "with #{plural(empty_cages, 'empty cage')}"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# handle magic script arguments
|
||||
here_only = $script_args.delete 'here'
|
||||
if here_only
|
||||
it = df.item_find
|
||||
list = [it]
|
||||
if not it.kind_of?(DFHack::ItemCagest) and not it.kind_of?(DFHack::ItemAnimaltrapst)
|
||||
list = df.world.items.other[:ANY_CAGE_OR_TRAP].find_all { |i| df.at_cursor?(i) }
|
||||
end
|
||||
puts 'Please select a cage' if list.empty?
|
||||
|
||||
elsif ids = $script_args.find_all { |arg| arg =~ /^\d+$/ } and ids.first
|
||||
list = []
|
||||
ids.each { |id|
|
||||
$script_args.delete id
|
||||
if not it = df.item_find(id.to_i)
|
||||
puts "Invalid item id #{id}"
|
||||
elsif not it.kind_of?(DFHack::ItemCagest) and not it.kind_of?(DFHack::ItemAnimaltrapst)
|
||||
puts "Item ##{id} is not a cage"
|
||||
list << it
|
||||
else
|
||||
list << it
|
||||
end
|
||||
}
|
||||
puts 'Please use a valid cage id' if list.empty?
|
||||
|
||||
else
|
||||
list = df.world.items.other[:ANY_CAGE_OR_TRAP]
|
||||
end
|
||||
|
||||
|
||||
# act
|
||||
case $script_args[0]
|
||||
when 'items'
|
||||
cage_dump_items(list) if not list.empty?
|
||||
when 'armor'
|
||||
cage_dump_armor(list) if not list.empty?
|
||||
when 'weapons'
|
||||
cage_dump_weapons(list) if not list.empty?
|
||||
when 'all'
|
||||
cage_dump_all(list) if not list.empty?
|
||||
|
||||
when 'list'
|
||||
cage_dump_list(list) if not list.empty?
|
||||
|
||||
else
|
||||
puts <<EOS
|
||||
Marks items inside all cages for dumping.
|
||||
Add 'here' to dump stuff only for selected cage.
|
||||
Add a cage id to dump stuff for this cage only.
|
||||
|
||||
See 'autodump' to actually dump stuff.
|
||||
|
||||
Usage:
|
||||
stripcaged items
|
||||
dump items directly in cages (eg seeds after training)
|
||||
|
||||
stripcaged [armor|weapons] here
|
||||
dump armor or weapons of caged creatures in selected cage
|
||||
|
||||
stripcaged all 28 29
|
||||
dump every item in cage id 28 and 29, along with every item worn by creatures in there too
|
||||
|
||||
stripcaged list
|
||||
show content of the cages
|
||||
|
||||
EOS
|
||||
|
||||
end
|