Skip to content

Commit 69b1bbe

Browse files
committed
Turn sorting lambdas into functors in autolabor.
1 parent b847a02 commit 69b1bbe

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

plugins/autolabor.cpp

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ static const df::job_skill noble_skills[] = {
384384
df::enums::job_skill::RECORD_KEEPING,
385385
};
386386

387-
struct dwarf_info
387+
struct dwarf_info_t
388388
{
389389
int highest_skill;
390390
int total_skill;
@@ -432,6 +432,38 @@ DFhackCExport command_result plugin_shutdown ( color_ostream &out )
432432
return CR_OK;
433433
}
434434

435+
// sorting objects
436+
struct dwarfinfo_sorter
437+
{
438+
dwarfinfo_sorter(std::vector <dwarf_info_t> & info):dwarf_info(info){};
439+
bool operator() (int i,int j)
440+
{
441+
if (dwarf_info[i].state == IDLE && dwarf_info[j].state != IDLE)
442+
return true;
443+
if (dwarf_info[i].state != IDLE && dwarf_info[j].state == IDLE)
444+
return false;
445+
return dwarf_info[i].mastery_penalty > dwarf_info[j].mastery_penalty;
446+
};
447+
std::vector <dwarf_info_t> & dwarf_info;
448+
};
449+
struct laborinfo_sorter
450+
{
451+
bool operator() (int i,int j)
452+
{
453+
return labor_infos[i].mode < labor_infos[j].mode;
454+
};
455+
};
456+
457+
struct values_sorter
458+
{
459+
values_sorter(std::vector <int> & values):values(values){};
460+
bool operator() (int i,int j)
461+
{
462+
return values[i] > values[j];
463+
};
464+
std::vector<int> & values;
465+
};
466+
435467
DFhackCExport command_result plugin_onupdate ( color_ostream &out )
436468
{
437469
static int step_count = 0;
@@ -478,7 +510,7 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
478510
if (n_dwarfs == 0)
479511
return CR_OK;
480512

481-
std::vector<dwarf_info> dwarf_info(n_dwarfs);
513+
std::vector<dwarf_info_t> dwarf_info(n_dwarfs);
482514

483515
std::vector<int> best_noble(ARRAY_COUNT(noble_skills));
484516
std::vector<int> highest_noble_skill(ARRAY_COUNT(noble_skills));
@@ -643,8 +675,8 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
643675

644676
labors.push_back(labor);
645677
}
646-
647-
std::sort(labors.begin(), labors.end(), [] (int i, int j) { return labor_infos[i].mode < labor_infos[j].mode; });
678+
laborinfo_sorter lasorter;
679+
std::sort(labors.begin(), labors.end(), lasorter);
648680

649681
// Handle all skills except those marked HAULERS
650682

@@ -734,7 +766,10 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
734766
}
735767

736768
if (labor_infos[labor].mode != EVERYONE)
737-
std::sort(candidates.begin(), candidates.end(), [&values] (int i, int j) { return values[i] > values[j]; });
769+
{
770+
values_sorter ivs(values);
771+
std::sort(candidates.begin(), candidates.end(), ivs);
772+
}
738773

739774
for (int dwarf = 0; dwarf < n_dwarfs; dwarf++)
740775
{
@@ -815,16 +850,9 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
815850
if (dwarf_info[dwarf].state == IDLE || dwarf_info[dwarf].state == BUSY)
816851
hauler_ids.push_back(dwarf);
817852
}
818-
853+
dwarfinfo_sorter sorter(dwarf_info);
819854
// Idle dwarves come first, then we sort from least-skilled to most-skilled.
820-
std::sort(hauler_ids.begin(), hauler_ids.end(), [&dwarf_info] (int i, int j) -> bool
821-
{
822-
if (dwarf_info[i].state == IDLE && dwarf_info[j].state != IDLE)
823-
return true;
824-
if (dwarf_info[i].state != IDLE && dwarf_info[j].state == IDLE)
825-
return false;
826-
return dwarf_info[i].mastery_penalty > dwarf_info[j].mastery_penalty;
827-
});
855+
std::sort(hauler_ids.begin(), hauler_ids.end(), sorter);
828856

829857
// don't set any haulers if everyone is off drinking or something
830858
if (hauler_ids.size() == 0) {

0 commit comments

Comments
 (0)