Update autohauler.cpp

develop
Fukken-Saved 2015-03-04 06:59:29 -05:00
parent ac5909ad2a
commit 36cee730f1
1 changed files with 72 additions and 41 deletions

@ -73,9 +73,9 @@ REQUIRE_GLOBAL(world);
* labors only being assigned to just one dwarf, no dwarf having more than two * labors only being assigned to just one dwarf, no dwarf having more than two
* active skilled labors, and almost every non-military dwarf having at least * active skilled labors, and almost every non-military dwarf having at least
* one skilled labor assigned. * one skilled labor assigned.
* In addition, autohauler allows skills to be flagged as to prevent hauling * Autohauler allows skills to be flagged as to prevent hauling labors from
* labors from being assigned when the skill is present. By default this is the * being assigned when the skill is present. By default this is the unused
* unused ALCHEMIST labor but can be changed by the user. * ALCHEMIST labor but can be changed by the user.
* It is noteworthy that, as stated in autolabor.cpp, "for almost all labors, * It is noteworthy that, as stated in autolabor.cpp, "for almost all labors,
* once a dwarf begins a job it will finish that job even if the associated * once a dwarf begins a job it will finish that job even if the associated
* labor is removed." This is why we can remove hauling labors by default to try * labor is removed." This is why we can remove hauling labors by default to try
@ -94,6 +94,12 @@ static PersistentDataItem config;
// later // later
static bool print_debug = false; static bool print_debug = false;
// Default number of frames between autohauler updates
const static int DEFAULT_FRAME_SKIP = 30;
// Number of frames between autohauler updates
static int frame_skip;
// 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);
@ -120,11 +126,10 @@ static void setOptionEnabled(ConfigFlags flag, bool on)
config.ival(0) &= ~flag; config.ival(0) &= ~flag;
} }
// Not sure what it does but it's probably related to the following enumeration // This is a vector of states and number of dwarves in that state
static std::vector<int> state_count(5); static std::vector<int> state_count(5);
// Employment status of dwarves // Employment status of dwarves
// xxx Shouldn't this be static?
enum dwarf_state { enum dwarf_state {
// Ready for a new task // Ready for a new task
IDLE, IDLE,
@ -143,8 +148,7 @@ enum dwarf_state {
}; };
// I presume this is the number of states in the following enumeration. // I presume this is the number of states in the following enumeration.
// xxx Shouldn't this be static? static const int NUM_STATE = 5;
const int NUM_STATE = 5;
// This is a list of strings to be associated with aforementioned dwarf_state // This is a list of strings to be associated with aforementioned dwarf_state
// struct // struct
@ -393,7 +397,6 @@ 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?
enum labor_mode { enum labor_mode {
ALLOW, ALLOW,
HAULERS, HAULERS,
@ -529,8 +532,6 @@ static void reset_labor(df::unit_labor labor)
*/ */
struct dwarf_info_t struct dwarf_info_t
{ {
// Total number of assigned jobs
int assigned_jobs;
// Current simplified employment status of dwarf // Current simplified employment status of dwarf
dwarf_state state; dwarf_state state;
// Set to true if for whatever reason we are exempting this dwarf // Set to true if for whatever reason we are exempting this dwarf
@ -555,7 +556,8 @@ static void init_state()
// This obtains the persistent data from the world save file // This obtains the persistent data from the world save file
config = World::GetPersistentData("autohauler/config"); config = World::GetPersistentData("autohauler/config");
// xxx I don't know what this does // Check to ensure that the persistent data item actually exists and that
// the first item in the array of ints isn't -1 (implies disabled)
if (config.isValid() && config.ival(0) == -1) if (config.isValid() && config.ival(0) == -1)
config.ival(0) = 0; config.ival(0) = 0;
@ -567,6 +569,21 @@ static void init_state()
if (!enable_autohauler) if (!enable_autohauler)
return; return;
// First get the frame skip from persistent data, or create the item
// if not present
auto cfg_frameskip = World::GetPersistentData("autohauler/frameskip");
if (cfg_frameskip.isValid())
{
frame_skip = cfg_frameskip.ival(0);
}
else
{
// Add to persistent data then get it to assert it's actually there
cfg_frameskip = World::AddPersistentData("autohauler/frameskip");
cfg_frameskip.ival(0) = DEFAULT_FRAME_SKIP;
frame_skip = cfg_frameskip.ival(0);
}
/* Here we are going to populate the labor list by loading persistent data /* Here we are going to populate the labor list by loading persistent data
* from the world save */ * from the world save */
@ -589,8 +606,6 @@ static void init_state()
// Translate the string into a labor defined by global dfhack constants // Translate the string into a labor defined by global dfhack constants
df::unit_labor labor = (df::unit_labor) atoi(key.substr(strlen("autohauler/labors/")).c_str()); df::unit_labor labor = (df::unit_labor) atoi(key.substr(strlen("autohauler/labors/")).c_str());
// This is a vector of all the current labor treatments
// Ensure that the labor is defined in the existing list // Ensure that the labor is defined in the existing list
if (labor >= 0 && labor <= labor_infos.size()) if (labor >= 0 && labor <= labor_infos.size())
{ {
@ -624,8 +639,7 @@ static void init_state()
reset_labor((df::unit_labor) i); reset_labor((df::unit_labor) i);
} }
// xxx I don't see the use of the following line // Set
// generate_labor_to_skill_map();
} }
@ -634,15 +648,14 @@ static void init_state()
*/ */
static void enable_plugin(color_ostream &out) static void enable_plugin(color_ostream &out)
{ {
// If there is no config file, make one // If there is no config persistent item, make one
if (!config.isValid()) if (!config.isValid())
{ {
config = World::AddPersistentData("autohauler/config"); config = World::AddPersistentData("autohauler/config");
config.ival(0) = 0; config.ival(0) = 0;
} }
// xxx I think this is already done in init_state(), but it can't hurt // I think this is already done in init_state(), but it can't hurt
// xxx Also, aren't these two redundant?
setOptionEnabled(CF_ENABLED, true); setOptionEnabled(CF_ENABLED, true);
enable_autohauler = true; enable_autohauler = true;
@ -687,6 +700,8 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <Plug
" Return a labor to the default handling.\n" " Return a labor to the default handling.\n"
" autohauler reset-all\n" " autohauler reset-all\n"
" Return all labors to the default handling.\n" " Return all labors to the default handling.\n"
" autohauler frameskip <int>\n"
" Set the number of frames between runs of autohauler.\n"
" autohauler list\n" " autohauler list\n"
" List current status of all labors.\n" " List current status of all labors.\n"
" autohauler status\n" " autohauler status\n"
@ -701,8 +716,10 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <Plug
"Examples:\n" "Examples:\n"
" autohauler HAUL_STONE haulers\n" " autohauler HAUL_STONE haulers\n"
" Set stone hauling as a hauling labor.\n" " Set stone hauling as a hauling labor.\n"
" autohauler FEED_WATER_CIVILIANS disable\n" " autohauler BOWYER allow\n"
" Disable plugin handling of feeding patients and prisoners.\n" " Allow hauling when the bowyer labor is enabled.\n"
" autohauler MINE forbid\n"
" Forbid hauling while the mining labor is disabled."
)); ));
// Initialize plugin labor lists // Initialize plugin labor lists
@ -712,7 +729,7 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <Plug
} }
/** /**
* Initialize the plugin * Shut down the plugin
*/ */
DFhackCExport command_result plugin_shutdown ( color_ostream &out ) DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{ {
@ -757,8 +774,7 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
step_count++; step_count++;
// Run aforementioned step count and return unless threshold is reached. // Run aforementioned step count and return unless threshold is reached.
// xxx We may want this to be a constant if (step_count < frame_skip) return CR_OK;
if (step_count < 60) return CR_OK;
// Reset step count since at this point it has reached 60 // Reset step count since at this point it has reached 60
step_count = 0; step_count = 0;
@ -846,11 +862,6 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
// 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
//if (dwarfs[dwarf]->specific_refs.size() > 0)
// dwarf_info[dwarf].state = OTHER;
// If no job is found then they are officially idle and open for hauling
//else
dwarf_info[dwarf].state = IDLE; dwarf_info[dwarf].state = IDLE;
} }
// If it gets to this point the dwarf is employed // If it gets to this point the dwarf is employed
@ -905,7 +916,10 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
{ {
if (dwarf_info[dwarf].state == IDLE || if (dwarf_info[dwarf].state == IDLE ||
dwarf_info[dwarf].state == BUSY || dwarf_info[dwarf].state == BUSY ||
dwarf_info[dwarf].state == OTHER) hauler_ids.push_back(dwarf); dwarf_info[dwarf].state == OTHER)
{
hauler_ids.push_back(dwarf);
}
} }
// Equivalent of Java for(unit_labor : labor) // Equivalent of Java for(unit_labor : labor)
@ -927,13 +941,6 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
// 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
if(!dwarfs[dwarf]->status.labors[labor])
{
dwarf_info[dwarf].assigned_jobs++;
}
// Increment number of times this labor was assigned
labor_infos[labor].active_dwarfs++;
// And enable the job for the dwarf // And enable the job for the dwarf
dwarfs[dwarf]->status.labors[labor] = true; dwarfs[dwarf]->status.labors[labor] = true;
} }
@ -942,6 +949,12 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
{ {
dwarfs[dwarf]->status.labors[labor] = false; dwarfs[dwarf]->status.labors[labor] = false;
} }
// If at the end of this the dwarf has the hauling labor, increment the
// counter
if(dwarfs[dwarf]->status.labors[labor])
{
labor_infos[labor].active_dwarfs++;
}
} }
@ -1000,8 +1013,7 @@ void print_labor (df::unit_labor labor, color_ostream &out)
} }
/** /**
* This appears to be miscellaneous bookkeping stuff, if I am lucky I will not * This responds to input from the command prompt.
* have to edit it.
*/ */
command_result autohauler (color_ostream &out, std::vector <std::string> & parameters) command_result autohauler (color_ostream &out, std::vector <std::string> & parameters)
{ {
@ -1020,6 +1032,23 @@ command_result autohauler (color_ostream &out, std::vector <std::string> & param
return plugin_enable(out, enable); return plugin_enable(out, enable);
} }
else if (parameters.size() == 2 && parameters[0] == "frameskip")
{
auto cfg_frameskip = World::GetPersistentData("autohauler/frameskip");
if(cfg_frameskip.isValid())
{
int newValue = atoi(parameters[1].c_str());
cfg_frameskip.ival(0) = newValue;
out << "Setting frame skip to " << newValue << endl;
frame_skip = cfg_frameskip.ival(0);
return CR_OK;
}
else
{
out << "Warning! No persistent data for frame skip!" << endl;
return CR_OK;
}
}
else if (parameters.size() >= 2 && parameters.size() <= 4) else if (parameters.size() >= 2 && parameters.size() <= 4)
{ {
if (!enable_autohauler) if (!enable_autohauler)
@ -1094,7 +1123,7 @@ command_result autohauler (color_ostream &out, std::vector <std::string> & param
return CR_FAILURE; return CR_FAILURE;
} }
bool need_comma = 0; bool need_comma = false;
for (int i = 0; i < NUM_STATE; i++) for (int i = 0; i < NUM_STATE; i++)
{ {
if (state_count[i] == 0) if (state_count[i] == 0)
@ -1102,10 +1131,12 @@ command_result autohauler (color_ostream &out, std::vector <std::string> & param
if (need_comma) if (need_comma)
out << ", "; out << ", ";
out << state_count[i] << ' ' << state_names[i]; out << state_count[i] << ' ' << state_names[i];
need_comma = 1; need_comma = true;
} }
out << endl; out << endl;
out << "Autohauler is running every " << frame_skip << " frames." << endl;
if (parameters[0] == "list") if (parameters[0] == "list")
{ {
FOR_ENUM_ITEMS(unit_labor, labor) FOR_ENUM_ITEMS(unit_labor, labor)