From 1566ae034f6832cae948fae59c600473075e01fd Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 27 Mar 2012 21:39:06 -0500 Subject: [PATCH 1/2] Fix crash in autolabor that happens when all dorfs decide to tipple at the same time --- plugins/autolabor.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/plugins/autolabor.cpp b/plugins/autolabor.cpp index 3ef0f9e5c..c87d6ffbf 100644 --- a/plugins/autolabor.cpp +++ b/plugins/autolabor.cpp @@ -802,7 +802,6 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) } // Idle dwarves come first, then we sort from least-skilled to most-skilled. - std::sort(hauler_ids.begin(), hauler_ids.end(), [&dwarf_info] (int i, int j) -> bool { if (dwarf_info[i].state == IDLE && dwarf_info[j].state != IDLE) @@ -812,6 +811,11 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) return dwarf_info[i].mastery_penalty > dwarf_info[j].mastery_penalty; }); + // don't set any haulers if everyone is off drinking or something + if (hauler_ids.size() == 0) { + num_haulers = 0; + } + FOR_ENUM_ITEMS(unit_labor, labor) { if (labor == df::enums::unit_labor::NONE) @@ -831,7 +835,6 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) assert(dwarf >= 0); assert(dwarf < n_dwarfs); - dwarfs[dwarf]->status.labors[labor] = true; dwarf_info[dwarf].assigned_jobs++; } @@ -848,7 +851,6 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out ) dwarfs[dwarf]->status.labors[labor] = false; } } - return CR_OK; } From 166392af62e2fd8c8e82b6195b116ddde9136ae4 Mon Sep 17 00:00:00 2001 From: Kelly Martin Date: Wed, 28 Mar 2012 10:25:36 -0500 Subject: [PATCH 2/2] Make autolabor accept "enable" and "disable" as well as "0" or "1". Add ability to set target number of miners in autolabor with "autolabor miners". --- plugins/autolabor.cpp | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/plugins/autolabor.cpp b/plugins/autolabor.cpp index c87d6ffbf..733f08066 100644 --- a/plugins/autolabor.cpp +++ b/plugins/autolabor.cpp @@ -300,7 +300,9 @@ struct labor_info int minimum_dwarfs; }; -static const struct labor_info labor_infos[] = { +static struct labor_info* labor_infos; + +static const struct labor_info default_labor_infos[] = { /* MINE */ {AUTOMATIC, true, 2}, /* HAUL_STONE */ {HAULERS, false, 1}, /* HAUL_WOOD */ {HAULERS, false, 1}, @@ -395,6 +397,13 @@ struct dwarf_info DFhackCExport command_result plugin_init ( color_ostream &out, std::vector &commands) { + // initialize labor infos table from default table + + labor_infos = new struct labor_info[ARRAY_COUNT(default_labor_infos)]; + for (int i = 0; i < ARRAY_COUNT(default_labor_infos); i++) { + labor_infos[i] = default_labor_infos[i]; + } + assert(ARRAY_COUNT(labor_infos) > ENUM_LAST_ITEM(unit_labor)); // Fill the command list with your commands. @@ -406,6 +415,8 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector \n" + " Set number of desired miners (defaults to 2)\n" "Function:\n" " When enabled, autolabor periodically checks your dwarves and enables or\n" " disables labors. It tries to keep as many dwarves as possible busy but\n" @@ -418,6 +429,9 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector & parameters) { - if (parameters.size() == 1 && (parameters[0] == "0" || parameters[0] == "1")) + if (parameters.size() == 1 && + (parameters[0] == "0" || parameters[0] == "enable" || + parameters[0] == "1" || parameters[0] == "disable")) { - if (parameters[0] == "0") + if (parameters[0] == "0" || parameters[0] == "disable") enable_autolabor = 0; else enable_autolabor = 1; out.print("autolabor %sactivated.\n", (enable_autolabor ? "" : "de")); } - else + else if (parameters.size() == 2 && parameters[0] == "miners") { + int nminers = atoi (parameters[1].c_str()); + if (nminers >= 0) { + labor_infos[0].minimum_dwarfs = nminers; + out.print("miner count set to %d.\n", nminers); + } else { + out.print("Syntax: autolabor miners , where n is 0 or more.\n" + "Current miner count: %d\n", labor_infos[0].minimum_dwarfs); + } + } else { out.print("Automatically assigns labors to dwarves.\n" "Activate with 'autolabor 1', deactivate with 'autolabor 0'.\n"