From 83f00e5583ed0dd98dcc0416971394be361cb641 Mon Sep 17 00:00:00 2001 From: Kelly Martin Date: Thu, 5 Jul 2012 09:59:28 -0500 Subject: [PATCH 01/11] Autolabor: allow setting the nonidle hauler percentage at runtime. Stripcaged: add keeparmor option --- plugins/autolabor.cpp | 37 ++++++++++++++++++++++++++++++++---- plugins/devel/stripcaged.cpp | 14 ++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/plugins/autolabor.cpp b/plugins/autolabor.cpp index de1a1aef6..e06e5eda8 100644 --- a/plugins/autolabor.cpp +++ b/plugins/autolabor.cpp @@ -383,6 +383,8 @@ struct labor_default int active_dwarfs; }; +static int hauler_pct = 33; + static std::vector labor_infos; static const struct labor_default default_labor_infos[] = { @@ -544,6 +546,16 @@ static void init_state() if (!enable_autolabor) return; + auto cfg_haulpct = pworld->GetPersistentData("autolabor/haulpct"); + if (cfg_haulpct.isValid()) + { + hauler_pct = cfg_haulpct.ival(0); + } + else + { + hauler_pct = 33; + } + // Load labors from save labor_infos.resize(ARRAY_COUNT(default_labor_infos)); @@ -956,6 +968,11 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) for (int dwarf = 0; dwarf < n_dwarfs; dwarf++) { + if (dwarf_info[dwarf].trader && trader_requested) + { + dwarfs[dwarf]->status.labors[labor] = false; + } + if (dwarfs[dwarf]->status.labors[labor]) { if (labor_infos[labor].is_exclusive) @@ -1041,6 +1058,10 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) value += 350; } + // bias by happiness + + value += dwarfs[dwarf]->status.happiness; + values[dwarf] = value; candidates.push_back(dwarf); @@ -1121,7 +1142,7 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) } if (print_debug) - out.print("Dwarf %i \"%s\" assigned %s: value %i\n", dwarf, dwarfs[dwarf]->name.first_name.c_str(), ENUM_KEY_STR(unit_labor, labor).c_str(), values[dwarf]); + out.print("Dwarf %i \"%s\" assigned %s: value %i\n %s", dwarf, dwarfs[dwarf]->name.first_name.c_str(), ENUM_KEY_STR(unit_labor, labor).c_str(), values[dwarf], dwarf_info[dwarf].trader ? "(trader)" : ""); if (dwarf_info[dwarf].state == IDLE || dwarf_info[dwarf].state == BUSY) labor_infos[labor].active_dwarfs++; @@ -1134,7 +1155,8 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) // Set about 1/3 of the dwarfs as haulers. The haulers have all HAULER labors enabled. Having a lot of haulers helps // make sure that hauling jobs are handled quickly rather than building up. - int num_haulers = state_count[IDLE] + state_count[BUSY] / 3; + int num_haulers = state_count[IDLE] + state_count[BUSY] * hauler_pct / 100; + if (num_haulers < 1) num_haulers = 1; @@ -1261,7 +1283,13 @@ command_result autolabor (color_ostream &out, std::vector & parame return CR_FAILURE; } - if (parameters.size() == 2 || parameters.size() == 3) { + else if (parameters.size() == 2 && parameters[0] == "haulpct") + { + int pct = atoi (parameters[1].c_str()); + hauler_pct = pct; + return CR_OK; + } + else if (parameters.size() == 2 || parameters.size() == 3) { df::enums::unit_labor::unit_labor labor = df::enums::unit_labor::NONE; FOR_ENUM_ITEMS(unit_labor, test_labor) @@ -1353,7 +1381,8 @@ command_result autolabor (color_ostream &out, std::vector & parame return CR_OK; } - else if (parameters.size() == 1 && parameters[0] == "debug") { + else if (parameters.size() == 1 && parameters[0] == "debug") + { print_debug = 1; return CR_OK; diff --git a/plugins/devel/stripcaged.cpp b/plugins/devel/stripcaged.cpp index 7e492cb01..922f220b9 100644 --- a/plugins/devel/stripcaged.cpp +++ b/plugins/devel/stripcaged.cpp @@ -77,6 +77,13 @@ DFhackCExport command_result plugin_shutdown ( color_ostream &out ) command_result df_stripcaged(color_ostream &out, vector & parameters) { CoreSuspender suspend; + bool keeparmor = false; + + if (parameters.size() == 1 && parameters[0] == "keeparmor") + { + out << "Not dumping armor" << endl; + keeparmor = true; + } size_t count = 0; for (size_t i=0; i < world->units.all.size(); i++) @@ -89,6 +96,13 @@ command_result df_stripcaged(color_ostream &out, vector & parameters) df::unit_inventory_item* uii = unit->inventory[j]; if (uii->item) { + if (keeparmor && (uii->item->isArmorNotClothing() || uii->item->isClothing())) + { + std::string desc; + uii->item->getItemDescription(&desc,0); + out << "Armor item " << desc << " not dumped" << endl; + continue; + } uii->item->flags.bits.forbid = 0; uii->item->flags.bits.dump = 1; count++; From 4c7c38df93a6600517157be50f1c964d4ba8805f Mon Sep 17 00:00:00 2001 From: Kelly Martin Date: Wed, 11 Jul 2012 14:30:47 -0500 Subject: [PATCH 02/11] * Autolabor: really exclude the broker from all labors when trader requested * Autolabor: add 'haulpct' config option to control percentage of non-idle dwarfs assigned to hauling labors (default is 33) * Zones: allow nontamed birds to be nestboxes. warning: does not check for 'hostile to civilization' birds, so if you try to tame a hostile elk bird, !!fun!! will happen * Stripcaged: changed default behavior to keep armor, reduced noisiness --- plugins/autolabor.cpp | 22 ++++++++++++++++++++-- plugins/devel/stripcaged.cpp | 16 +++++++--------- plugins/zone.cpp | 4 ++-- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/plugins/autolabor.cpp b/plugins/autolabor.cpp index cf460e1cc..2dffaafe2 100644 --- a/plugins/autolabor.cpp +++ b/plugins/autolabor.cpp @@ -754,7 +754,12 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) df::building_tradedepotst* depot = (df::building_tradedepotst*) build; trader_requested = depot->trade_flags.bits.trader_requested; if (print_debug) - out.print("Trade depot found and trader requested, trader will be excluded from all labors.\n"); + { + if (trader_requested) + out.print("Trade depot found and trader requested, trader will be excluded from all labors.\n"); + else + out.print("Trade depot found but trader is not requested.\n"); + } } } @@ -1016,6 +1021,8 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) continue; if (dwarf_info[dwarf].state == MILITARY) continue; + if (dwarf_info[dwarf].trader && trader_requested) + continue; if (labor_infos[labor].is_exclusive && dwarf_info[dwarf].has_exclusive_labor) continue; @@ -1142,7 +1149,7 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) } if (print_debug) - out.print("Dwarf %i \"%s\" assigned %s: value %i\n %s", dwarf, dwarfs[dwarf]->name.first_name.c_str(), ENUM_KEY_STR(unit_labor, labor).c_str(), values[dwarf], dwarf_info[dwarf].trader ? "(trader)" : ""); + out.print("Dwarf %i \"%s\" assigned %s: value %i %s\n", dwarf, dwarfs[dwarf]->name.first_name.c_str(), ENUM_KEY_STR(unit_labor, labor).c_str(), values[dwarf], dwarf_info[dwarf].trader ? "(trader)" : ""); if (dwarf_info[dwarf].state == IDLE || dwarf_info[dwarf].state == BUSY) labor_infos[labor].active_dwarfs++; @@ -1164,7 +1171,18 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) for (int dwarf = 0; dwarf < n_dwarfs; dwarf++) { if (dwarf_info[dwarf].trader && trader_requested) + { + FOR_ENUM_ITEMS(unit_labor, labor) + { + if (labor == df::enums::unit_labor::NONE) + continue; + if (labor_infos[labor].mode() != HAULERS) + continue; + dwarfs[dwarf]->status.labors[labor] = false; + } continue; + } + if (dwarf_info[dwarf].state == IDLE || dwarf_info[dwarf].state == BUSY) hauler_ids.push_back(dwarf); } diff --git a/plugins/devel/stripcaged.cpp b/plugins/devel/stripcaged.cpp index 922f220b9..e3d2a82fc 100644 --- a/plugins/devel/stripcaged.cpp +++ b/plugins/devel/stripcaged.cpp @@ -77,12 +77,12 @@ DFhackCExport command_result plugin_shutdown ( color_ostream &out ) command_result df_stripcaged(color_ostream &out, vector & parameters) { CoreSuspender suspend; - bool keeparmor = false; + bool keeparmor = true; - if (parameters.size() == 1 && parameters[0] == "keeparmor") + if (parameters.size() == 1 && parameters[0] == "dumparmor") { - out << "Not dumping armor" << endl; - keeparmor = true; + out << "Dumping armor too" << endl; + keeparmor = false; } size_t count = 0; @@ -97,12 +97,10 @@ command_result df_stripcaged(color_ostream &out, vector & parameters) if (uii->item) { if (keeparmor && (uii->item->isArmorNotClothing() || uii->item->isClothing())) - { - std::string desc; - uii->item->getItemDescription(&desc,0); - out << "Armor item " << desc << " not dumped" << endl; continue; - } + std::string desc; + uii->item->getItemDescription(&desc,0); + out << "Item " << desc << " dumped." << endl; uii->item->flags.bits.forbid = 0; uii->item->flags.bits.dump = 1; count++; diff --git a/plugins/zone.cpp b/plugins/zone.cpp index ce610128b..52ea5c07b 100644 --- a/plugins/zone.cpp +++ b/plugins/zone.cpp @@ -420,6 +420,7 @@ bool isTame(df::unit* creature) { switch (creature->training_level) { + case df::animal_training_level::SemiWild: //?? case df::animal_training_level::Trained: case df::animal_training_level::WellTrained: case df::animal_training_level::SkilfullyTrained: @@ -429,7 +430,6 @@ bool isTame(df::unit* creature) case df::animal_training_level::Domesticated: tame=true; break; - case df::animal_training_level::SemiWild: //?? case df::animal_training_level::Unk8: //?? case df::animal_training_level::WildUntamed: default: @@ -1232,7 +1232,7 @@ bool isFreeEgglayer(df::unit * unit) { if( !isDead(unit) && !isUndead(unit) && isFemale(unit) - && isDomesticated(unit) // better strict than sorry (medium trained wild animals can revert into wild state) + && isTame(unit) && isOwnCiv(unit) && isEggLayer(unit) && !isAssigned(unit) From b2623e68a3c1c75e1727227f56783ec3adf20dff Mon Sep 17 00:00:00 2001 From: Kelly Martin Date: Wed, 11 Jul 2012 14:38:04 -0500 Subject: [PATCH 03/11] Track submodules --- depends/clsocket | 2 +- plugins/stonesense | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/depends/clsocket b/depends/clsocket index d0b2d0750..c85e9fb35 160000 --- a/depends/clsocket +++ b/depends/clsocket @@ -1 +1 @@ -Subproject commit d0b2d0750dc2d529a152eba4f3f519f69ff7eab0 +Subproject commit c85e9fb35d3510c5dcc367056cda3237d77a7add diff --git a/plugins/stonesense b/plugins/stonesense index 5d4f06d78..17b653665 160000 --- a/plugins/stonesense +++ b/plugins/stonesense @@ -1 +1 @@ -Subproject commit 5d4f06d785f8a9933679fe3caa12c18215e9674d +Subproject commit 17b653665567a5f1df628217820f76bb0b9c70a5 From 2cafe540d588683495d879eb712ae4a7dacda4c2 Mon Sep 17 00:00:00 2001 From: Kelly Martin Date: Wed, 11 Jul 2012 14:42:21 -0500 Subject: [PATCH 04/11] Add more ruby cruft to .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index ceb0aa27a..85fc083e7 100644 --- a/.gitignore +++ b/.gitignore @@ -52,7 +52,9 @@ dfhack/python/dist # Ruby binding binaries plugins/ruby/libruby* +plugins/ruby/msvcrtruby*.tar.gz plugins/ruby/ruby-autogen.rb +plugins/ruby/ruby-autogen.rb.rule # CPack stuff build/CPack*Config.cmake From b0edb330233e7a5ae0db9f079ad5db487eabcd20 Mon Sep 17 00:00:00 2001 From: jj Date: Wed, 11 Jul 2012 23:24:53 +0200 Subject: [PATCH 05/11] ruby: fix unit_idlers --- plugins/ruby/unit.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/ruby/unit.rb b/plugins/ruby/unit.rb index 04deee0e3..5b279d0d1 100644 --- a/plugins/ruby/unit.rb +++ b/plugins/ruby/unit.rb @@ -63,7 +63,7 @@ module DFHack # current_job includes eat/drink/sleep/pickupequip !u.job.current_job and # filter 'attend meeting' - u.meetings.length == 0 and + not u.specific_refs.find { |s| s.type == :ACTIVITY } and # filter soldiers (TODO check schedule) u.military.squad_index == -1 and # filter 'on break' From d8e55196c265e457ca47c5117be8abd493e907ac Mon Sep 17 00:00:00 2001 From: jj Date: Wed, 11 Jul 2012 23:25:55 +0200 Subject: [PATCH 06/11] ruby: better handling of errors in onupdate callbacks --- plugins/ruby/ruby.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/ruby/ruby.rb b/plugins/ruby/ruby.rb index 5ae63ebfe..8c2c97969 100644 --- a/plugins/ruby/ruby.rb +++ b/plugins/ruby/ruby.rb @@ -38,15 +38,17 @@ module DFHack @callback.call else if year > @minyear or (year == @minyear and yeartick >= @minyeartick) - @callback.call @minyear = year @minyeartick = yeartick + @ticklimit if @minyeartick > yearlen @minyear += 1 @minyeartick -= yearlen end + @callback.call end end + rescue + puts_err "onupdate cb #$!", $!.backtrace end def <=>(o) From 0615a27663adb72990abc9e3084b09df552a35bc Mon Sep 17 00:00:00 2001 From: jj Date: Thu, 12 Jul 2012 00:44:07 +0200 Subject: [PATCH 07/11] ruby: refix unit_idlers, make unit_find handle 'u'nitlist viewscreen --- plugins/ruby/unit.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/plugins/ruby/unit.rb b/plugins/ruby/unit.rb index 5b279d0d1..ebcf249da 100644 --- a/plugins/ruby/unit.rb +++ b/plugins/ruby/unit.rb @@ -6,9 +6,15 @@ module DFHack # with an argument that respond to x/y/z (eg cursor), find first unit at this position def unit_find(what=:selected, y=nil, z=nil) if what == :selected - if curview._rtti_classname == :viewscreen_itemst + case curview._rtti_classname + when :viewscreen_itemst ref = curview.entry_ref[curview.cursor_pos] ref.unit_tg if ref.kind_of?(GeneralRefUnit) + when :viewscreen_unitlistst + v = curview + # TODO fix xml to use enums everywhere + page = DFHack::ViewscreenUnitlistst_TPage.int(v.page) + v.units[page][v.cursor_pos[page]] else case ui.main.mode when :ViewUnits @@ -67,7 +73,7 @@ module DFHack # filter soldiers (TODO check schedule) u.military.squad_index == -1 and # filter 'on break' - !u.status.misc_traits.find { |t| id == :OnBreak } + not u.status.misc_traits.find { |t| t.id == :OnBreak } } end From c823f1273737a18bf0df9720c287c30bb9f5623c Mon Sep 17 00:00:00 2001 From: jj Date: Thu, 12 Jul 2012 14:35:49 +0200 Subject: [PATCH 08/11] add scripts/magmasource to generate an infinite magma source --- scripts/magmasource.rb | 65 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 scripts/magmasource.rb diff --git a/scripts/magmasource.rb b/scripts/magmasource.rb new file mode 100644 index 000000000..14f517fd1 --- /dev/null +++ b/scripts/magmasource.rb @@ -0,0 +1,65 @@ +# create an infinite magma source at the cursor + +$magma_sources ||= [] +$magma_onupdate ||= nil + +case $script_args[0] +when 'here' + $magma_onupdate ||= df.onupdate_register(12) { + if $magma_sources.empty? + df.onupdate_unregister($magma_onupdate) + $magma_onupdate = nil + end + + $magma_sources.each { |x, y, z| + if tile = df.map_tile_at(x, y, z) and DFHack::TiletypeShape::PassableFlow[tile.shape] + des = tile.designation + des.flow_size += 1 if des.flow_size < 7 + des.liquid_type = 1 + des.flow_forbid = true + + mf = tile.mapblock.flags + mf.update_liquid = true + mf.update_liquid_twice = true + + zf = df.world.map.z_level_flags[z] + zf.update = true + zf.update_twice = true + end + } + } + + if df.cursor.x != -30000 + if tile = df.map_tile_at(df.cursor) + if DFHack::TiletypeShape::PassableFlow[tile.shape] + $magma_sources << [df.cursor.x, df.cursor.y, df.cursor.z] + else + puts "Impassable tile: I'm afraid I can't do that, Dave" + end + else + puts "Unallocated map block - build something here first" + end + else + puts "Please put the game cursor where you want a magma source" + end + +when 'delete-here' + $magma_sources.delete [df.cursor.x, df.cursor.y, df.cursor.z] + +when 'stop' + $magma_sources.clear + +else + puts < Date: Thu, 12 Jul 2012 14:50:59 +0200 Subject: [PATCH 09/11] magmasource: add documentation --- README.rst | 24 ++++++++++++++++++++---- scripts/magmasource.rb | 7 ++++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index 9543e63a0..724fa1fa3 100644 --- a/README.rst +++ b/README.rst @@ -1377,8 +1377,6 @@ For exemple, to grow 40 plump helmet spawn: growcrops plump 40 -This is a ruby script and needs the ruby plugin. - removebadthoughts ================= @@ -1396,8 +1394,6 @@ you unpause. With the optional ``-v`` parameter, the script will dump the negative thoughts it removed. -This is a ruby script and needs the ruby plugin. - slayrace ======== @@ -1418,3 +1414,23 @@ after selecting the unit with the 'v' cursor: :: rb_eval df.unit_find.body.blood_count = 0 + +magmasource +=========== +Create an infinite magma source on a tile. + +This script registers a map tile as a magma source, and every 12 game ticks +that tile receives 1 new unit of flowing magma. + +Place the game cursor where you want to create the source (must be a +flow-passable tile, and not too high in the sky) and call +:: + magmasource here + +To add more than 1 unit everytime, call the command again. + +To delete one source, place the cursor over its tile and use ``delete-here``. +To remove all placed sources, call ``magmasource stop``. + +With no argument, this command shows an help message and list existing sources. + diff --git a/scripts/magmasource.rb b/scripts/magmasource.rb index 14f517fd1..8525d51e0 100644 --- a/scripts/magmasource.rb +++ b/scripts/magmasource.rb @@ -1,11 +1,11 @@ # create an infinite magma source at the cursor $magma_sources ||= [] -$magma_onupdate ||= nil case $script_args[0] when 'here' $magma_onupdate ||= df.onupdate_register(12) { + # called every 12 game ticks (100x a dwarf day) if $magma_sources.empty? df.onupdate_unregister($magma_onupdate) $magma_onupdate = nil @@ -52,14 +52,15 @@ when 'stop' else puts < Date: Mon, 16 Jul 2012 20:56:46 -0500 Subject: [PATCH 10/11] Synchronize with changes to df-structures --- plugins/fixpositions.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/plugins/fixpositions.cpp b/plugins/fixpositions.cpp index 086848fec..bd8fb2791 100644 --- a/plugins/fixpositions.cpp +++ b/plugins/fixpositions.cpp @@ -80,9 +80,9 @@ command_result df_fixdiplomats (color_ostream &out, vector ¶meters) pos->flags.set(entity_position_flags::IS_DIPLOMAT); pos->flags.set(entity_position_flags::IS_LEADER); // not sure what these flags do, but the game sets them for a valid diplomat - pos->flags.set(entity_position_flags::anon_1); - pos->flags.set(entity_position_flags::anon_3); - pos->flags.set(entity_position_flags::anon_4); + pos->flags.set(entity_position_flags::unk_12); + pos->flags.set(entity_position_flags::unk_1a); + pos->flags.set(entity_position_flags::unk_1b); pos->name[0] = "Diplomat"; pos->name[1] = "Diplomats"; pos->precedence = 70; @@ -183,9 +183,9 @@ command_result df_fixmerchants (color_ostream &out, vector ¶meters) pos->flags.set(entity_position_flags::IS_DIPLOMAT); pos->flags.set(entity_position_flags::IS_LEADER); // not sure what these flags do, but the game sets them for a valid guild rep - pos->flags.set(entity_position_flags::anon_1); - pos->flags.set(entity_position_flags::anon_3); - pos->flags.set(entity_position_flags::anon_4); + pos->flags.set(entity_position_flags::unk_12); + pos->flags.set(entity_position_flags::unk_1a); + pos->flags.set(entity_position_flags::unk_1b); pos->name[0] = "Guild Representative"; pos->name[1] = "Guild Representatives"; pos->precedence = 40; From 78fc850ce20e14d7a37e44c18dc4486ee61796b4 Mon Sep 17 00:00:00 2001 From: Kelly Martin Date: Sun, 22 Jul 2012 12:16:50 -0500 Subject: [PATCH 11/11] Autolabor: dwarves who are scheduled for a meeting are automatically cleared of all labors. Labor to skill map is now generated at plugin start rather than with every iteration. Also partially refactored the code; no behavioral changes from that. --- plugins/autolabor.cpp | 641 ++++++++++++++++++++++++++++++------------ 1 file changed, 457 insertions(+), 184 deletions(-) diff --git a/plugins/autolabor.cpp b/plugins/autolabor.cpp index 2dffaafe2..fd72bd635 100644 --- a/plugins/autolabor.cpp +++ b/plugins/autolabor.cpp @@ -31,9 +31,16 @@ #include #include #include +#include +#include +#include +#include #include +#include "modules/MapCache.h" +#include "modules/Items.h" + using std::string; using std::endl; using namespace DFHack; @@ -89,6 +96,8 @@ command_result autolabor (color_ostream &out, std::vector & parame // The name string provided must correspond to the filename - autolabor.plug.so or autolabor.plug.dll in this case DFHACK_PLUGIN("autolabor"); +void generate_labor_to_skill_map(); + enum labor_mode { DISABLE, HAULERS, @@ -503,6 +512,8 @@ struct dwarf_info_t int noble_penalty; // penalty for assignment due to noble status bool medical; // this dwarf has medical responsibility bool trader; // this dwarf has trade responsibility + bool diplomacy; // this dwarf meets with diplomats + int single_labor; // this dwarf will be exclusively assigned to one labor (-1/NONE for none) }; static bool isOptionEnabled(unsigned flag) @@ -588,8 +599,37 @@ static void init_state() labor_infos[i].active_dwarfs = 0; reset_labor((df::enums::unit_labor::unit_labor) i); } + + generate_labor_to_skill_map(); + } +static df::job_skill labor_to_skill[ENUM_LAST_ITEM(unit_labor) + 1]; + +static void generate_labor_to_skill_map() +{ + // Generate labor -> skill mapping + + for (int i = 0; i <= ENUM_LAST_ITEM(unit_labor); i++) + labor_to_skill[i] = df::enums::job_skill::NONE; + + FOR_ENUM_ITEMS(job_skill, skill) + { + int labor = ENUM_ATTR(job_skill, labor, skill); + if (labor != df::enums::unit_labor::NONE) + { + /* + assert(labor >= 0); + assert(labor < ARRAY_COUNT(labor_to_skill)); + */ + + labor_to_skill[labor] = skill; + } + } + +} + + static void enable_plugin(color_ostream &out) { auto pworld = Core::getInstance().getWorld(); @@ -697,6 +737,182 @@ struct values_sorter std::vector & values; }; + +static void assign_labor(unit_labor::unit_labor labor, + int n_dwarfs, + std::vector& dwarf_info, + bool trader_requested, + std::vector& dwarfs, + bool has_butchers, + bool has_fishery, + color_ostream& out) +{ + df::job_skill skill = labor_to_skill[labor]; + + if (labor_infos[labor].mode() != AUTOMATIC) + return; + + int best_dwarf = 0; + int best_value = -10000; + + std::vector values(n_dwarfs); + std::vector candidates; + std::map dwarf_skill; + std::vector previously_enabled(n_dwarfs); + + auto mode = labor_infos[labor].mode(); + + // Find candidate dwarfs, and calculate a preference value for each dwarf + for (int dwarf = 0; dwarf < n_dwarfs; dwarf++) + { + if (dwarf_info[dwarf].state == CHILD) + continue; + if (dwarf_info[dwarf].state == MILITARY) + continue; + if (dwarf_info[dwarf].trader && trader_requested) + continue; + if (dwarf_info[dwarf].diplomacy) + continue; + + if (labor_infos[labor].is_exclusive && dwarf_info[dwarf].has_exclusive_labor) + continue; + + int value = dwarf_info[dwarf].mastery_penalty; + + if (skill != df::enums::job_skill::NONE) + { + int skill_level = 0; + int skill_experience = 0; + + for (auto s = dwarfs[dwarf]->status.souls[0]->skills.begin(); s < dwarfs[dwarf]->status.souls[0]->skills.end(); s++) + { + if ((*s)->id == skill) + { + skill_level = (*s)->rating; + skill_experience = (*s)->experience; + break; + } + } + + dwarf_skill[dwarf] = skill_level; + + value += skill_level * 100; + value += skill_experience / 20; + if (skill_level > 0 || skill_experience > 0) + value += 200; + if (skill_level >= 15) + value += 1000 * (skill_level - 14); + } + else + { + dwarf_skill[dwarf] = 0; + } + + if (dwarfs[dwarf]->status.labors[labor]) + { + value += 5; + if (labor_infos[labor].is_exclusive) + value += 350; + } + + // bias by happiness + + value += dwarfs[dwarf]->status.happiness; + + values[dwarf] = value; + + candidates.push_back(dwarf); + + } + + // Sort candidates by preference value + values_sorter ivs(values); + std::sort(candidates.begin(), candidates.end(), ivs); + + // Disable the labor on everyone + for (int dwarf = 0; dwarf < n_dwarfs; dwarf++) + { + if (dwarf_info[dwarf].state == CHILD) + continue; + + previously_enabled[dwarf] = dwarfs[dwarf]->status.labors[labor]; + dwarfs[dwarf]->status.labors[labor] = false; + } + + int min_dwarfs = labor_infos[labor].minimum_dwarfs(); + int max_dwarfs = labor_infos[labor].maximum_dwarfs(); + + // Special - don't assign hunt without a butchers, or fish without a fishery + if (df::enums::unit_labor::HUNT == labor && !has_butchers) + min_dwarfs = max_dwarfs = 0; + if (df::enums::unit_labor::FISH == labor && !has_fishery) + min_dwarfs = max_dwarfs = 0; + + bool want_idle_dwarf = true; + if (state_count[IDLE] < 2) + want_idle_dwarf = false; + + /* + * Assign dwarfs to this labor. We assign at least the minimum number of dwarfs, in + * order of preference, and then assign additional dwarfs that meet any of these conditions: + * - The dwarf is idle and there are no idle dwarves assigned to this labor + * - The dwarf has nonzero skill associated with the labor + * - The labor is mining, hunting, or woodcutting and the dwarf currently has it enabled. + * We stop assigning dwarfs when we reach the maximum allowed. + * Note that only idle and busy dwarfs count towards the number of dwarfs. "Other" dwarfs + * (sleeping, eating, on break, etc.) will have labors assigned, but will not be counted. + * Military and children/nobles will not have labors assigned. + * Dwarfs with the "health management" responsibility are always assigned DIAGNOSIS. + */ + for (int i = 0; i < candidates.size() && labor_infos[labor].active_dwarfs < max_dwarfs; i++) + { + int dwarf = candidates[i]; + + assert(dwarf >= 0); + assert(dwarf < n_dwarfs); + + bool preferred_dwarf = false; + if (want_idle_dwarf && dwarf_info[dwarf].state == IDLE) + preferred_dwarf = true; + if (dwarf_skill[dwarf] > 0) + preferred_dwarf = true; + if (previously_enabled[dwarf] && labor_infos[labor].is_exclusive) + preferred_dwarf = true; + if (dwarf_info[dwarf].medical && labor == df::unit_labor::DIAGNOSE) + preferred_dwarf = true; + if (dwarf_info[dwarf].trader && trader_requested) + continue; + if (dwarf_info[dwarf].diplomacy) + continue; + + if (labor_infos[labor].active_dwarfs >= min_dwarfs && !preferred_dwarf) + continue; + + if (!dwarfs[dwarf]->status.labors[labor]) + dwarf_info[dwarf].assigned_jobs++; + + dwarfs[dwarf]->status.labors[labor] = true; + + if (labor_infos[labor].is_exclusive) + { + dwarf_info[dwarf].has_exclusive_labor = true; + // all the exclusive labors require equipment so this should force the dorf to reequip if needed + dwarfs[dwarf]->military.pickup_flags.bits.update = 1; + } + + if (print_debug) + out.print("Dwarf %i \"%s\" assigned %s: value %i %s %s\n", dwarf, dwarfs[dwarf]->name.first_name.c_str(), ENUM_KEY_STR(unit_labor, labor).c_str(), values[dwarf], dwarf_info[dwarf].trader ? "(trader)" : "", dwarf_info[dwarf].diplomacy ? "(diplomacy)" : ""); + + if (dwarf_info[dwarf].state == IDLE || dwarf_info[dwarf].state == BUSY) + labor_infos[labor].active_dwarfs++; + + if (dwarf_info[dwarf].state == IDLE) + want_idle_dwarf = false; + } + +} + + DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) { switch (event) { @@ -767,7 +983,10 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) { df::unit* cre = world->units.all[i]; if (cre->race == race && cre->civ_id == civ && !cre->flags1.bits.marauder && !cre->flags1.bits.diplomat && !cre->flags1.bits.merchant && - !cre->flags1.bits.dead && !cre->flags1.bits.forest) { + !cre->flags1.bits.dead && !cre->flags1.bits.forest) + { + if (cre->burrows.size() > 0) + continue; // dwarfs assigned to burrows are skipped entirely dwarfs.push_back(cre); } } @@ -783,6 +1002,8 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) for (int dwarf = 0; dwarf < n_dwarfs; dwarf++) { + dwarf_info[dwarf].single_labor = -1; + // assert(dwarfs[dwarf]->status.souls.size() > 0); // assert fails can cause DF to crash, so don't do that @@ -794,9 +1015,11 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) int noble_penalty = 0; df::historical_figure* hf = df::historical_figure::find(dwarfs[dwarf]->hist_figure_id); - for (int i = 0; i < hf->entity_links.size(); i++) { + for (int i = 0; i < hf->entity_links.size(); i++) + { df::histfig_entity_link* hfelink = hf->entity_links.at(i); - if (hfelink->getType() == df::histfig_entity_link_type::POSITION) { + if (hfelink->getType() == df::histfig_entity_link_type::POSITION) + { df::histfig_entity_link_positionst *epos = (df::histfig_entity_link_positionst*) hfelink; df::historical_entity* entity = df::historical_entity::find(epos->entity_id); @@ -820,9 +1043,29 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) dwarf_info[dwarf].trader = true; } - dwarf_info[dwarf].noble_penalty = noble_penalty; + } + dwarf_info[dwarf].noble_penalty = noble_penalty; + + // identify dwarfs who are needed for meetings and mark them for exclusion + + for (int i = 0; i < ui->activities.size(); ++i) + { + df::activity_info *act = ui->activities[i]; + if (!act) continue; + bool p1 = act->person1 == dwarfs[dwarf]; + bool p2 = act->person2 == dwarfs[dwarf]; + + if (p1 || p2) + { + dwarf_info[dwarf].diplomacy = true; + if (print_debug) + out.print("Dwarf %i \"%s\" has a meeting, will be cleared of all labors\n", dwarf, dwarfs[dwarf]->name.first_name.c_str()); + break; + } + } + for (auto s = dwarfs[dwarf]->status.souls[0]->skills.begin(); s != dwarfs[dwarf]->status.souls[0]->skills.end(); s++) { df::job_skill skill = (*s)->id; @@ -924,26 +1167,6 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) out.print("Dwarf %i \"%s\": penalty %i, state %s\n", dwarf, dwarfs[dwarf]->name.first_name.c_str(), dwarf_info[dwarf].mastery_penalty, state_names[dwarf_info[dwarf].state]); } - // Generate labor -> skill mapping - - df::job_skill labor_to_skill[ENUM_LAST_ITEM(unit_labor) + 1]; - for (int i = 0; i <= ENUM_LAST_ITEM(unit_labor); i++) - labor_to_skill[i] = df::enums::job_skill::NONE; - - FOR_ENUM_ITEMS(job_skill, skill) - { - int labor = ENUM_ATTR(job_skill, labor, skill); - if (labor != df::enums::unit_labor::NONE) - { - /* - assert(labor >= 0); - assert(labor < ARRAY_COUNT(labor_to_skill)); - */ - - labor_to_skill[labor] = skill; - } - } - std::vector labors; FOR_ENUM_ITEMS(unit_labor, labor) @@ -973,7 +1196,8 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) for (int dwarf = 0; dwarf < n_dwarfs; dwarf++) { - if (dwarf_info[dwarf].trader && trader_requested) + if ((dwarf_info[dwarf].trader && trader_requested) || + dwarf_info[dwarf].diplomacy) { dwarfs[dwarf]->status.labors[labor] = false; } @@ -999,164 +1223,7 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) assert(labor < ARRAY_COUNT(labor_infos)); */ - df::job_skill skill = labor_to_skill[labor]; - - if (labor_infos[labor].mode() != AUTOMATIC) - continue; - - int best_dwarf = 0; - int best_value = -10000; - - std::vector values(n_dwarfs); - std::vector candidates; - std::map dwarf_skill; - std::vector previously_enabled(n_dwarfs); - - auto mode = labor_infos[labor].mode(); - - // Find candidate dwarfs, and calculate a preference value for each dwarf - for (int dwarf = 0; dwarf < n_dwarfs; dwarf++) - { - if (dwarf_info[dwarf].state == CHILD) - continue; - if (dwarf_info[dwarf].state == MILITARY) - continue; - if (dwarf_info[dwarf].trader && trader_requested) - continue; - - if (labor_infos[labor].is_exclusive && dwarf_info[dwarf].has_exclusive_labor) - continue; - - int value = dwarf_info[dwarf].mastery_penalty; - - if (skill != df::enums::job_skill::NONE) - { - int skill_level = 0; - int skill_experience = 0; - - for (auto s = dwarfs[dwarf]->status.souls[0]->skills.begin(); s < dwarfs[dwarf]->status.souls[0]->skills.end(); s++) - { - if ((*s)->id == skill) - { - skill_level = (*s)->rating; - skill_experience = (*s)->experience; - break; - } - } - - dwarf_skill[dwarf] = skill_level; - - value += skill_level * 100; - value += skill_experience / 20; - if (skill_level > 0 || skill_experience > 0) - value += 200; - if (skill_level >= 15) - value += 1000 * (skill_level - 14); - } - else - { - dwarf_skill[dwarf] = 0; - } - - if (dwarfs[dwarf]->status.labors[labor]) - { - value += 5; - if (labor_infos[labor].is_exclusive) - value += 350; - } - - // bias by happiness - - value += dwarfs[dwarf]->status.happiness; - - values[dwarf] = value; - - candidates.push_back(dwarf); - - } - - // Sort candidates by preference value - values_sorter ivs(values); - std::sort(candidates.begin(), candidates.end(), ivs); - - // Disable the labor on everyone - for (int dwarf = 0; dwarf < n_dwarfs; dwarf++) - { - if (dwarf_info[dwarf].state == CHILD) - continue; - - previously_enabled[dwarf] = dwarfs[dwarf]->status.labors[labor]; - dwarfs[dwarf]->status.labors[labor] = false; - } - - int min_dwarfs = labor_infos[labor].minimum_dwarfs(); - int max_dwarfs = labor_infos[labor].maximum_dwarfs(); - - // Special - don't assign hunt without a butchers, or fish without a fishery - if (df::enums::unit_labor::HUNT == labor && !has_butchers) - min_dwarfs = max_dwarfs = 0; - if (df::enums::unit_labor::FISH == labor && !has_fishery) - min_dwarfs = max_dwarfs = 0; - - bool want_idle_dwarf = true; - if (state_count[IDLE] < 2) - want_idle_dwarf = false; - - /* - * Assign dwarfs to this labor. We assign at least the minimum number of dwarfs, in - * order of preference, and then assign additional dwarfs that meet any of these conditions: - * - The dwarf is idle and there are no idle dwarves assigned to this labor - * - The dwarf has nonzero skill associated with the labor - * - The labor is mining, hunting, or woodcutting and the dwarf currently has it enabled. - * We stop assigning dwarfs when we reach the maximum allowed. - * Note that only idle and busy dwarfs count towards the number of dwarfs. "Other" dwarfs - * (sleeping, eating, on break, etc.) will have labors assigned, but will not be counted. - * Military and children/nobles will not have labors assigned. - * Dwarfs with the "health management" responsibility are always assigned DIAGNOSIS. - */ - for (int i = 0; i < candidates.size() && labor_infos[labor].active_dwarfs < max_dwarfs; i++) - { - int dwarf = candidates[i]; - - assert(dwarf >= 0); - assert(dwarf < n_dwarfs); - - bool preferred_dwarf = false; - if (want_idle_dwarf && dwarf_info[dwarf].state == IDLE) - preferred_dwarf = true; - if (dwarf_skill[dwarf] > 0) - preferred_dwarf = true; - if (previously_enabled[dwarf] && labor_infos[labor].is_exclusive) - preferred_dwarf = true; - if (dwarf_info[dwarf].medical && labor == df::unit_labor::DIAGNOSE) - preferred_dwarf = true; - if (dwarf_info[dwarf].trader && trader_requested) - continue; - - if (labor_infos[labor].active_dwarfs >= min_dwarfs && !preferred_dwarf) - continue; - - if (!dwarfs[dwarf]->status.labors[labor]) - dwarf_info[dwarf].assigned_jobs++; - - dwarfs[dwarf]->status.labors[labor] = true; - - if (labor_infos[labor].is_exclusive) - { - dwarf_info[dwarf].has_exclusive_labor = true; - // all the exclusive labors require equipment so this should force the dorf to reequip if needed - dwarfs[dwarf]->military.pickup_flags.bits.update = 1; - } - - if (print_debug) - out.print("Dwarf %i \"%s\" assigned %s: value %i %s\n", dwarf, dwarfs[dwarf]->name.first_name.c_str(), ENUM_KEY_STR(unit_labor, labor).c_str(), values[dwarf], dwarf_info[dwarf].trader ? "(trader)" : ""); - - if (dwarf_info[dwarf].state == IDLE || dwarf_info[dwarf].state == BUSY) - labor_infos[labor].active_dwarfs++; - - if (dwarf_info[dwarf].state == IDLE) - want_idle_dwarf = false; - } + assign_labor(labor, n_dwarfs, dwarf_info, trader_requested, dwarfs, has_butchers, has_fishery, out); } // Set about 1/3 of the dwarfs as haulers. The haulers have all HAULER labors enabled. Having a lot of haulers helps @@ -1170,7 +1237,8 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) std::vector hauler_ids; for (int dwarf = 0; dwarf < n_dwarfs; dwarf++) { - if (dwarf_info[dwarf].trader && trader_requested) + if ((dwarf_info[dwarf].trader && trader_requested) || + dwarf_info[dwarf].diplomacy) { FOR_ENUM_ITEMS(unit_labor, labor) { @@ -1262,6 +1330,7 @@ void print_labor (df::enums::unit_labor::unit_labor labor, color_ostream &out) } } + command_result autolabor (color_ostream &out, std::vector & parameters) { CoreSuspender suspend; @@ -1414,3 +1483,207 @@ command_result autolabor (color_ostream &out, std::vector & parame return CR_OK; } } + +struct StockpileInfo { + df::building_stockpilest* sp; + int size; + int free; + int x1, x2, y1, y2, z; + +public: + StockpileInfo(df::building_stockpilest *sp_) : sp(sp_) + { + MapExtras::MapCache mc; + + z = sp_->z; + x1 = sp_->room.x; + x2 = sp_->room.x + sp_->room.width; + y1 = sp_->room.y; + y2 = sp_->room.y + sp_->room.height; + int e = 0; + size = 0; + free = 0; + for (int y = y1; y < y2; y++) + for (int x = x1; x < x2; x++) + if (sp_->room.extents[e++] == 1) + { + size++; + DFCoord cursor (x,y,z); + uint32_t blockX = x / 16; + uint32_t tileX = x % 16; + uint32_t blockY = y / 16; + uint32_t tileY = y % 16; + MapExtras::Block * b = mc.BlockAt(cursor/16); + if(b && b->is_valid()) + { + auto &block = *b->getRaw(); + df::tile_occupancy &occ = block.occupancy[tileX][tileY]; + if (!occ.bits.item) + free++; + } + } + } + + bool isFull() { return free == 0; } + + bool canHold(df::item *i) + { + return false; + } + + bool inStockpile(df::item *i) + { + df::item *container = Items::getContainer(i); + if (container) + return inStockpile(container); + + if (i->pos.z != z) return false; + if (i->pos.x < x1 || i->pos.x >= x2 || + i->pos.y < y1 || i->pos.y >= y2) return false; + int e = (i->pos.x - x1) + (i->pos.y - y1) * sp->room.width; + return sp->room.extents[e] == 1; + } + + int getId() { return sp->id; } +}; + +static int stockcheck(color_ostream &out, vector & parameters) +{ + int count = 0; + + std::vector stockpiles; + + for (int i = 0; i < world->buildings.all.size(); ++i) + { + df::building *build = world->buildings.all[i]; + auto type = build->getType(); + if (df::enums::building_type::Stockpile == type) + { + df::building_stockpilest *sp = virtual_cast(build); + StockpileInfo *spi = new StockpileInfo(sp); + stockpiles.push_back(spi); + } + + } + + std::vector &items = world->items.other[df::enums::items_other_id::ANY_FREE]; + + // 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(artifact1); + F(spider_web); F(owned); F(in_job); +#undef F + + for (size_t i = 0; i < items.size(); i++) + { + df::item *item = items[i]; + if (item->flags.whole & bad_flags.whole) + continue; + + // we really only care about MEAT, FISH, FISH_RAW, PLANT, CHEESE, FOOD, and EGG + + df::item_type typ = item->getType(); + if (typ != df::enums::item_type::MEAT && + typ != df::enums::item_type::FISH && + typ != df::enums::item_type::FISH_RAW && + typ != df::enums::item_type::PLANT && + typ != df::enums::item_type::CHEESE && + typ != df::enums::item_type::FOOD && + typ != df::enums::item_type::EGG) + continue; + + df::item *container = 0; + df::unit *holder = 0; + df::building *building = 0; + + for (size_t i = 0; i < item->itemrefs.size(); i++) + { + df::general_ref *ref = item->itemrefs[i]; + + switch (ref->getType()) + { + case general_ref_type::CONTAINED_IN_ITEM: + container = ref->getItem(); + break; + + case general_ref_type::UNIT_HOLDER: + holder = ref->getUnit(); + break; + + case general_ref_type::BUILDING_HOLDER: + building = ref->getBuilding(); + break; + + default: + break; + } + } + + df::item *nextcontainer = container; + df::item *lastcontainer = 0; + + while(nextcontainer) { + df::item *thiscontainer = nextcontainer; + nextcontainer = 0; + for (size_t i = 0; i < thiscontainer->itemrefs.size(); i++) + { + df::general_ref *ref = thiscontainer->itemrefs[i]; + + switch (ref->getType()) + { + case general_ref_type::CONTAINED_IN_ITEM: + lastcontainer = nextcontainer = ref->getItem(); + break; + + case general_ref_type::UNIT_HOLDER: + holder = ref->getUnit(); + break; + + case general_ref_type::BUILDING_HOLDER: + building = ref->getBuilding(); + break; + + default: + break; + } + } + } + + if (holder) + continue; // carried items do not rot as far as i know + + if (building) { + df::building_type btype = building->getType(); + if (btype == df::enums::building_type::TradeDepot || + btype == df::enums::building_type::Wagon) + continue; // items in trade depot or the embark wagon do not rot + + if (typ == df::enums::item_type::EGG && btype ==df::enums::building_type::NestBox) + continue; // eggs in nest box do not rot + } + + int canHoldCount = 0; + StockpileInfo *current = 0; + + for (int idx = 0; idx < stockpiles.size(); idx++) + { + StockpileInfo *spi = stockpiles[idx]; + if (spi->canHold(item)) canHoldCount++; + if (spi->inStockpile(item)) current=spi; + } + + if (current) + continue; + + count++; + + } + + return count; +} +