Update autohauler.cpp

develop
Fukken-Saved 2015-02-25 07:22:25 -05:00
parent 68d088eeb5
commit 53c88bab56
1 changed files with 172 additions and 137 deletions

@ -60,7 +60,7 @@ REQUIRE_GLOBAL(world);
/* /*
* Autohauler module for dfhack * Autohauler module for dfhack
* Fork of autolabor * Fork of autolabor, DFHack version
* *
* Rather than the all-of-the-above means of autolabor, autohauler will instead * Rather than the all-of-the-above means of autolabor, autohauler will instead
* only manage hauling labors and leave skilled labors entirely to the user, who * only manage hauling labors and leave skilled labors entirely to the user, who
@ -87,6 +87,10 @@ DFHACK_PLUGIN_IS_ENABLED(enable_autohauler);
// This is the configuration saved into the world save file // This is the configuration saved into the world save file
static PersistentDataItem config; static PersistentDataItem config;
// There is a possibility I will add extensive, line-by-line debug capability
// later
static bool print_debug = false;
// Don't know what this does // Don't know what this does
command_result autohauler (color_ostream &out, std::vector <std::string> & parameters); command_result autohauler (color_ostream &out, std::vector <std::string> & parameters);
@ -113,10 +117,6 @@ static void setOptionEnabled(ConfigFlags flag, bool on)
config.ival(0) &= ~flag; config.ival(0) &= ~flag;
} }
// There is a possibility I will add extensive, line-by-line debug capability
// later
static bool print_debug = false;
// Not sure what it does but it's probably related to the following enumeration // Not sure what it does but it's probably related to the following enumeration
static std::vector<int> state_count(5); static std::vector<int> state_count(5);
@ -392,8 +392,9 @@ static const dwarf_state dwarf_states[] = {
// Mode assigned to labors. Either it's a hauling job, or it's not. // Mode assigned to labors. Either it's a hauling job, or it's not.
// xxx Shouldn't this be static? // xxx Shouldn't this be static?
enum labor_mode { enum labor_mode {
DISABLE, ALLOW,
HAULERS, HAULERS,
FORBID
}; };
// This is the default treatment of a particular labor. // This is the default treatment of a particular labor.
@ -419,7 +420,6 @@ struct labor_info
// Return the labor_mode associated with this labor // Return the labor_mode associated with this labor
labor_mode mode() { return (labor_mode) config.ival(0); } labor_mode mode() { return (labor_mode) config.ival(0); }
// Set the labor_mode associated with this labor // Set the labor_mode associated with this labor
void set_mode(labor_mode mode) { config.ival(0) = mode; } void set_mode(labor_mode mode) { config.ival(0) = mode; }
}; };
@ -430,7 +430,7 @@ static std::vector<struct labor_info> labor_infos;
// This is just an array of all the labors, whether it should be untouched // This is just an array of all the labors, whether it should be untouched
// (DISABLE) or treated as a last-resort job (HAULERS). // (DISABLE) or treated as a last-resort job (HAULERS).
static const struct labor_default default_labor_infos[] = { static const struct labor_default default_labor_infos[] = {
/* MINE */ {DISABLE, 0}, /* MINE */ {ALLOW, 0},
/* HAUL_STONE */ {HAULERS, 0}, /* HAUL_STONE */ {HAULERS, 0},
/* HAUL_WOOD */ {HAULERS, 0}, /* HAUL_WOOD */ {HAULERS, 0},
/* HAUL_BODY */ {HAULERS, 0}, /* HAUL_BODY */ {HAULERS, 0},
@ -440,75 +440,75 @@ static const struct labor_default default_labor_infos[] = {
/* HAUL_FURNITURE */ {HAULERS, 0}, /* HAUL_FURNITURE */ {HAULERS, 0},
/* HAUL_ANIMAL */ {HAULERS, 0}, /* HAUL_ANIMAL */ {HAULERS, 0},
/* CLEAN */ {HAULERS, 0}, /* CLEAN */ {HAULERS, 0},
/* CUTWOOD */ {DISABLE, 0}, /* CUTWOOD */ {ALLOW, 0},
/* CARPENTER */ {DISABLE, 0}, /* CARPENTER */ {ALLOW, 0},
/* DETAIL */ {DISABLE, 0}, /* DETAIL */ {ALLOW, 0},
/* MASON */ {DISABLE, 0}, /* MASON */ {ALLOW, 0},
/* ARCHITECT */ {DISABLE, 0}, /* ARCHITECT */ {ALLOW, 0},
/* ANIMALTRAIN */ {DISABLE, 0}, /* ANIMALTRAIN */ {ALLOW, 0},
/* ANIMALCARE */ {DISABLE, 0}, /* ANIMALCARE */ {ALLOW, 0},
/* DIAGNOSE */ {DISABLE, 0}, /* DIAGNOSE */ {ALLOW, 0},
/* SURGERY */ {DISABLE, 0}, /* SURGERY */ {ALLOW, 0},
/* BONE_SETTING */ {DISABLE, 0}, /* BONE_SETTING */ {ALLOW, 0},
/* SUTURING */ {DISABLE, 0}, /* SUTURING */ {ALLOW, 0},
/* DRESSING_WOUNDS */ {DISABLE, 0}, /* DRESSING_WOUNDS */ {ALLOW, 0},
/* FEED_WATER_CIVILIANS */ {DISABLE, 0}, /* FEED_WATER_CIVILIANS */ {ALLOW, 0},
/* RECOVER_WOUNDED */ {HAULERS, 0}, /* RECOVER_WOUNDED */ {HAULERS, 0},
/* BUTCHER */ {DISABLE, 0}, /* BUTCHER */ {ALLOW, 0},
/* TRAPPER */ {DISABLE, 0}, /* TRAPPER */ {ALLOW, 0},
/* DISSECT_VERMIN */ {DISABLE, 0}, /* DISSECT_VERMIN */ {ALLOW, 0},
/* LEATHER */ {DISABLE, 0}, /* LEATHER */ {ALLOW, 0},
/* TANNER */ {DISABLE, 0}, /* TANNER */ {ALLOW, 0},
/* BREWER */ {DISABLE, 0}, /* BREWER */ {ALLOW, 0},
/* ALCHEMIST */ {DISABLE, 0}, /* ALCHEMIST */ {FORBID, 0},
/* SOAP_MAKER */ {DISABLE, 0}, /* SOAP_MAKER */ {ALLOW, 0},
/* WEAVER */ {DISABLE, 0}, /* WEAVER */ {ALLOW, 0},
/* CLOTHESMAKER */ {DISABLE, 0}, /* CLOTHESMAKER */ {ALLOW, 0},
/* MILLER */ {DISABLE, 0}, /* MILLER */ {ALLOW, 0},
/* PROCESS_PLANT */ {DISABLE, 0}, /* PROCESS_PLANT */ {ALLOW, 0},
/* MAKE_CHEESE */ {DISABLE, 0}, /* MAKE_CHEESE */ {ALLOW, 0},
/* MILK */ {DISABLE, 0}, /* MILK */ {ALLOW, 0},
/* COOK */ {DISABLE, 0}, /* COOK */ {ALLOW, 0},
/* PLANT */ {DISABLE, 0}, /* PLANT */ {ALLOW, 0},
/* HERBALIST */ {DISABLE, 0}, /* HERBALIST */ {ALLOW, 0},
/* FISH */ {DISABLE, 0}, /* FISH */ {ALLOW, 0},
/* CLEAN_FISH */ {DISABLE, 0}, /* CLEAN_FISH */ {ALLOW, 0},
/* DISSECT_FISH */ {DISABLE, 0}, /* DISSECT_FISH */ {ALLOW, 0},
/* HUNT */ {DISABLE, 0}, /* HUNT */ {ALLOW, 0},
/* SMELT */ {DISABLE, 0}, /* SMELT */ {ALLOW, 0},
/* FORGE_WEAPON */ {DISABLE, 0}, /* FORGE_WEAPON */ {ALLOW, 0},
/* FORGE_ARMOR */ {DISABLE, 0}, /* FORGE_ARMOR */ {ALLOW, 0},
/* FORGE_FURNITURE */ {DISABLE, 0}, /* FORGE_FURNITURE */ {ALLOW, 0},
/* METAL_CRAFT */ {DISABLE, 0}, /* METAL_CRAFT */ {ALLOW, 0},
/* CUT_GEM */ {DISABLE, 0}, /* CUT_GEM */ {ALLOW, 0},
/* ENCRUST_GEM */ {DISABLE, 0}, /* ENCRUST_GEM */ {ALLOW, 0},
/* WOOD_CRAFT */ {DISABLE, 0}, /* WOOD_CRAFT */ {ALLOW, 0},
/* STONE_CRAFT */ {DISABLE, 0}, /* STONE_CRAFT */ {ALLOW, 0},
/* BONE_CARVE */ {DISABLE, 0}, /* BONE_CARVE */ {ALLOW, 0},
/* GLASSMAKER */ {DISABLE, 0}, /* GLASSMAKER */ {ALLOW, 0},
/* EXTRACT_STRAND */ {DISABLE, 0}, /* EXTRACT_STRAND */ {ALLOW, 0},
/* SIEGECRAFT */ {DISABLE, 0}, /* SIEGECRAFT */ {ALLOW, 0},
/* SIEGEOPERATE */ {DISABLE, 0}, /* SIEGEOPERATE */ {ALLOW, 0},
/* BOWYER */ {DISABLE, 0}, /* BOWYER */ {ALLOW, 0},
/* MECHANIC */ {DISABLE, 0}, /* MECHANIC */ {ALLOW, 0},
/* POTASH_MAKING */ {DISABLE, 0}, /* POTASH_MAKING */ {ALLOW, 0},
/* LYE_MAKING */ {DISABLE, 0}, /* LYE_MAKING */ {ALLOW, 0},
/* DYER */ {DISABLE, 0}, /* DYER */ {ALLOW, 0},
/* BURN_WOOD */ {DISABLE, 0}, /* BURN_WOOD */ {ALLOW, 0},
/* OPERATE_PUMP */ {DISABLE, 0}, /* OPERATE_PUMP */ {ALLOW, 0},
/* SHEARER */ {DISABLE, 0}, /* SHEARER */ {ALLOW, 0},
/* SPINNER */ {DISABLE, 0}, /* SPINNER */ {ALLOW, 0},
/* POTTERY */ {DISABLE, 0}, /* POTTERY */ {ALLOW, 0},
/* GLAZING */ {DISABLE, 0}, /* GLAZING */ {ALLOW, 0},
/* PRESSING */ {DISABLE, 0}, /* PRESSING */ {ALLOW, 0},
/* BEEKEEPING */ {DISABLE, 0}, /* BEEKEEPING */ {ALLOW, 0},
/* WAX_WORKING */ {DISABLE, 0}, /* WAX_WORKING */ {ALLOW, 0},
/* HANDLE_VEHICLES */ {HAULERS, 0}, /* HANDLE_VEHICLES */ {HAULERS, 0},
/* HAUL_TRADE */ {HAULERS, 0}, /* HAUL_TRADE */ {HAULERS, 0},
/* PULL_LEVER */ {HAULERS, 0}, /* PULL_LEVER */ {HAULERS, 0},
/* REMOVE_CONSTRUCTION */ {HAULERS, 0}, /* REMOVE_CONSTRUCTION */ {HAULERS, 0},
/* HAUL_WATER */ {HAULERS, 0}, /* HAUL_WATER */ {HAULERS, 0},
/* GELD */ {DISABLE, 0}, /* GELD */ {ALLOW, 0},
/* BUILD_ROAD */ {HAULERS, 0}, /* BUILD_ROAD */ {HAULERS, 0},
/* BUILD_CONSTRUCTION */ {HAULERS, 0} /* BUILD_CONSTRUCTION */ {HAULERS, 0}
}; };
@ -522,12 +522,19 @@ static void reset_labor(df::unit_labor labor)
} }
/** /**
* A * This is individualized dwarf info populated in plugin_onupdate
*/ */
struct dwarf_info_t struct dwarf_info_t
{ {
// Total number of assigned jobs
int assigned_jobs; int assigned_jobs;
// Current simplified employment status of dwarf
dwarf_state state; dwarf_state state;
// Set to true if the dwarf is on break or a migrant
bool is_on_break;
// Set to true if for whatever reason we are exempting this dwarf
// from hauling
bool haul_exempt;
}; };
/** /**
@ -589,7 +596,7 @@ static void init_state()
// Link the labor treatment with the associated persistent data item // Link the labor treatment with the associated persistent data item
labor_infos[labor].set_config(*p); labor_infos[labor].set_config(*p);
// Set the number of dwarves associated with labor to zero
labor_infos[labor].active_dwarfs = 0; labor_infos[labor].active_dwarfs = 0;
} }
} }
@ -671,8 +678,10 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <Plug
" Enables or disables the plugin.\n" " Enables or disables the plugin.\n"
" autohauler <labor> haulers\n" " autohauler <labor> haulers\n"
" Set a labor to be handled by hauler dwarves.\n" " Set a labor to be handled by hauler dwarves.\n"
" autohauler <labor> disable\n" " autohauler <labor> allowed\n"
" Turn off autohauler for a specific labor.\n" " Allow hauling if a specific labor is enabled.\n"
" autohauler <labor> forbid\n"
" Forbid hauling if a specific labor is enabled.\n"
" autohauler <labor> reset\n" " autohauler <labor> reset\n"
" Return a labor to the default handling.\n" " Return a labor to the default handling.\n"
" autohauler reset-all\n" " autohauler reset-all\n"
@ -791,34 +800,51 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
// Find the activity state for each dwarf // Find the activity state for each dwarf
for (int dwarf = 0; dwarf < n_dwarfs; dwarf++) for (int dwarf = 0; dwarf < n_dwarfs; dwarf++)
{ {
/* Before determining how to handle employment status, handle
* hauling exemptions first */
// Scan every labor. If a labor that disallows hauling is present
// for the dwarf, the dwarf is hauling exempt
FOR_ENUM_ITEMS(unit_labor, labor)
{
if (!(labor == unit_labor::NONE))
{
bool test1 = labor_infos[labor].mode() == FORBID;
bool test2 = dwarfs[dwarf]->status.labors[labor];
if(test1 && test2) dwarf_info[dwarf].haul_exempt = true;
}
}
// Default deny condition of on break for later else-if series if(print_debug) out.print("Yeah, it gets past here\n");
bool is_on_break = false;
// Scan a dwarf's miscellaneous traits for on break or migrant status. // Scan a dwarf's miscellaneous traits for on break or migrant status.
// If either of these are present, disable hauling because we want them // If either of these are present, disable hauling because we want them
// to try to find real jobs first // to try to find real jobs first
for (auto p = dwarfs[dwarf]->status.misc_traits.begin(); p < dwarfs[dwarf]->status.misc_traits.end(); p++) for (auto p = dwarfs[dwarf]->status.misc_traits.begin(); p < dwarfs[dwarf]->status.misc_traits.end(); p++)
{ {
if ((*p)->id == misc_trait_type::Migrant || (*p)->id == misc_trait_type::OnBreak) if ((*p)->id == misc_trait_type::Migrant || (*p)->id == misc_trait_type::OnBreak)
is_on_break = true; dwarf_info[dwarf].haul_exempt = true;
} }
// I don't think you can set the labors for babies and children, but let's /* Now determine a dwarf's employment status and decide whether
* to assign hauling */
// I don't think you can set the labors for babies and children, but let's
// ignore them anyway // ignore them anyway
if (Units::isBaby(dwarfs[dwarf]) || Units::isChild(dwarfs[dwarf])) if (Units::isBaby(dwarfs[dwarf]) || Units::isChild(dwarfs[dwarf]))
{ {
dwarf_info[dwarf].state = CHILD; dwarf_info[dwarf].state = CHILD;
} }
// Don't give hauling jobs to dwarves on break or migrants // Account for any hauling exemptions here
else if (is_on_break) else if (dwarf_info[dwarf].haul_exempt)
{ {
dwarf_info[dwarf].state = OTHER; dwarf_info[dwarf].state = OTHER;
} }
// Don't give hauling jobs to the military either // Don't give hauling jobs to the military either
else if (ENUM_ATTR(profession, military, dwarfs[dwarf]->profession)) else if (ENUM_ATTR(profession, military, dwarfs[dwarf]->profession))
dwarf_info[dwarf].state = MILITARY; dwarf_info[dwarf].state = MILITARY;
// Dwarf is unemployed with null job // Dwarf is unemployed with null job
else if (dwarfs[dwarf]->job.current_job == NULL) else if (dwarfs[dwarf]->job.current_job == NULL)
{ {
// xxx Figure out what specific_refs is // xxx Figure out what specific_refs is
@ -898,18 +924,18 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
// For every dwarf... // For every dwarf...
for(int dwarf = 0; dwarf < dwarfs.size(); dwarf++) for(int dwarf = 0; dwarf < dwarfs.size(); dwarf++)
{ {
// xxx I don't think this was necessary?
// int dwarf = dwarfs[i];
// If the dwarf is idle, enable the hauling labor // If the dwarf is idle, enable the hauling labor
if(dwarf_info[dwarf].state == IDLE) if(dwarf_info[dwarf].state == IDLE)
{ {
// Only increment assignment counter if job wasn't present before // Only increment assignment counter if job wasn't present before
if(!dwarfs[dwarf]->status.labors[labor]) if(!dwarfs[dwarf]->status.labors[labor])
{ {
dwarf_info[dwarf].assigned_jobs++; dwarf_info[dwarf].assigned_jobs++;
} }
// Increment number of times this labor was assigned
labor_infos[labor].active_dwarfs++; labor_infos[labor].active_dwarfs++;
// And enable the job for the dwarf
dwarfs[dwarf]->status.labors[labor] = true; dwarfs[dwarf]->status.labors[labor] = true;
} }
// If the dwarf is busy, disable the hauling labor // If the dwarf is busy, disable the hauling labor
@ -920,8 +946,8 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
} }
// Let's play a game of "find the missing bracket!" I hope this is correct. // Let's play a game of "find the missing bracket!" I hope this is correct.
} }
// This would be the last statement of the method // This would be the last statement of the method
return CR_OK; return CR_OK;
@ -962,12 +988,15 @@ void print_labor (df::unit_labor labor, color_ostream &out)
out << labor_name << ": "; out << labor_name << ": ";
for (int i = 0; i < 20 - (int)labor_name.length(); i++) for (int i = 0; i < 20 - (int)labor_name.length(); i++)
out << ' '; out << ' ';
if (labor_infos[labor].mode() == DISABLE) if (labor_infos[labor].mode() == ALLOW) out << "allow" << endl;
out << "disabled" << endl; else if(labor_infos[labor].mode() == FORBID) out << "forbid" << endl;
else if(labor_infos[labor].mode() == HAULERS)
{
out << "haulers, currently " << labor_infos[labor].active_dwarfs << " dwarfs" << endl;
}
else else
{ {
if (labor_infos[labor].mode() == HAULERS) out << "Warning: Invalid labor mode!" << endl;
out << "haulers, currently " << labor_infos[labor].active_dwarfs << " dwarfs" << endl;
} }
} }
@ -1020,9 +1049,15 @@ command_result autohauler (color_ostream &out, std::vector <std::string> & param
print_labor(labor, out); print_labor(labor, out);
return CR_OK; return CR_OK;
} }
if (parameters[1] == "disable") if (parameters[1] == "allow")
{
labor_infos[labor].set_mode(ALLOW);
print_labor(labor, out);
return CR_OK;
}
if (parameters[1] == "forbid")
{ {
labor_infos[labor].set_mode(DISABLE); labor_infos[labor].set_mode(FORBID);
print_labor(labor, out); print_labor(labor, out);
return CR_OK; return CR_OK;
} }
@ -1093,7 +1128,7 @@ command_result autohauler (color_ostream &out, std::vector <std::string> & param
return CR_FAILURE; return CR_FAILURE;
} }
print_debug = 1; print_debug = true;
return CR_OK; return CR_OK;
} }