Turn sorting lambdas into functors in autolabor.

develop
Petr Mrázek 2012-03-29 20:00:26 +02:00
parent b847a026e3
commit 69b1bbe59e
1 changed files with 42 additions and 14 deletions

@ -384,7 +384,7 @@ static const df::job_skill noble_skills[] = {
df::enums::job_skill::RECORD_KEEPING,
};
struct dwarf_info
struct dwarf_info_t
{
int highest_skill;
int total_skill;
@ -432,6 +432,38 @@ DFhackCExport command_result plugin_shutdown ( color_ostream &out )
return CR_OK;
}
// sorting objects
struct dwarfinfo_sorter
{
dwarfinfo_sorter(std::vector <dwarf_info_t> & info):dwarf_info(info){};
bool operator() (int i,int j)
{
if (dwarf_info[i].state == IDLE && dwarf_info[j].state != IDLE)
return true;
if (dwarf_info[i].state != IDLE && dwarf_info[j].state == IDLE)
return false;
return dwarf_info[i].mastery_penalty > dwarf_info[j].mastery_penalty;
};
std::vector <dwarf_info_t> & dwarf_info;
};
struct laborinfo_sorter
{
bool operator() (int i,int j)
{
return labor_infos[i].mode < labor_infos[j].mode;
};
};
struct values_sorter
{
values_sorter(std::vector <int> & values):values(values){};
bool operator() (int i,int j)
{
return values[i] > values[j];
};
std::vector<int> & values;
};
DFhackCExport command_result plugin_onupdate ( color_ostream &out )
{
static int step_count = 0;
@ -478,7 +510,7 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
if (n_dwarfs == 0)
return CR_OK;
std::vector<dwarf_info> dwarf_info(n_dwarfs);
std::vector<dwarf_info_t> dwarf_info(n_dwarfs);
std::vector<int> best_noble(ARRAY_COUNT(noble_skills));
std::vector<int> highest_noble_skill(ARRAY_COUNT(noble_skills));
@ -643,8 +675,8 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
labors.push_back(labor);
}
std::sort(labors.begin(), labors.end(), [] (int i, int j) { return labor_infos[i].mode < labor_infos[j].mode; });
laborinfo_sorter lasorter;
std::sort(labors.begin(), labors.end(), lasorter);
// Handle all skills except those marked HAULERS
@ -734,7 +766,10 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
}
if (labor_infos[labor].mode != EVERYONE)
std::sort(candidates.begin(), candidates.end(), [&values] (int i, int j) { return values[i] > values[j]; });
{
values_sorter ivs(values);
std::sort(candidates.begin(), candidates.end(), ivs);
}
for (int dwarf = 0; dwarf < n_dwarfs; dwarf++)
{
@ -815,16 +850,9 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
if (dwarf_info[dwarf].state == IDLE || dwarf_info[dwarf].state == BUSY)
hauler_ids.push_back(dwarf);
}
dwarfinfo_sorter sorter(dwarf_info);
// 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)
return true;
if (dwarf_info[i].state != IDLE && dwarf_info[j].state == IDLE)
return false;
return dwarf_info[i].mastery_penalty > dwarf_info[j].mastery_penalty;
});
std::sort(hauler_ids.begin(), hauler_ids.end(), sorter);
// don't set any haulers if everyone is off drinking or something
if (hauler_ids.size() == 0) {