Skip to content

Commit ac71fa8

Browse files
committed
Add isValidLabor() to Units module
Closes #711 and could avoid potential issues when editing units from other civs with manipulator.
1 parent ff4f720 commit ac71fa8

6 files changed

Lines changed: 27 additions & 16 deletions

File tree

library/LuaApi.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,7 @@ static const LuaWrapper::FunctionReg dfhack_units_module[] = {
15191519
WRAPM(Units, getNominalSkill),
15201520
WRAPM(Units, getEffectiveSkill),
15211521
WRAPM(Units, getExperience),
1522+
WRAPM(Units, isValidLabor),
15221523
WRAPM(Units, computeMovementSpeed),
15231524
WRAPM(Units, computeSlowdownFactor),
15241525
WRAPM(Units, getProfessionName),

library/include/modules/Units.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ DFHACK_EXPORT int getNominalSkill(df::unit *unit, df::job_skill skill_id, bool u
269269
DFHACK_EXPORT int getEffectiveSkill(df::unit *unit, df::job_skill skill_id);
270270
DFHACK_EXPORT int getExperience(df::unit *unit, df::job_skill skill_id, bool total = false);
271271

272+
DFHACK_EXPORT bool isValidLabor(df::unit *unit, df::unit_labor labor);
273+
272274
DFHACK_EXPORT int computeMovementSpeed(df::unit *unit);
273275
DFHACK_EXPORT float computeSlowdownFactor(df::unit *unit);
274276

library/modules/Units.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,19 @@ int Units::getEffectiveSkill(df::unit *unit, df::job_skill skill_id)
12231223
return rating;
12241224
}
12251225

1226+
bool Units::isValidLabor(df::unit *unit, df::unit_labor labor)
1227+
{
1228+
CHECK_NULL_POINTER(unit);
1229+
if (!is_valid_enum_item(labor))
1230+
return false;
1231+
if (labor == df::unit_labor::NONE)
1232+
return false;
1233+
df::historical_entity *entity = df::historical_entity::find(unit->civ_id);
1234+
if (entity && entity->entity_raw && !entity->entity_raw->jobs.permitted_labor[labor])
1235+
return false;
1236+
return true;
1237+
}
1238+
12261239
inline void adjust_speed_rating(int &rating, bool is_adventure, int value, int dwarf100, int dwarf200, int adv50, int adv75, int adv100, int adv200)
12271240
{
12281241
if (is_adventure)

plugins/autohauler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,8 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
951951
// For every dwarf...
952952
for(int dwarf = 0; dwarf < dwarfs.size(); dwarf++)
953953
{
954+
if (!Units::isValidLabor(dwarfs[dwarf], labor))
955+
continue;
954956

955957
// Set hauling labors based on employment states
956958
if(dwarf_info[dwarf].state == IDLE) {

plugins/autolabor.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ static void enable_plugin(color_ostream &out)
658658

659659
setOptionEnabled(CF_ENABLED, true);
660660
enable_autolabor = true;
661-
out << "Enabling the plugin." << endl;
661+
out << "Enabling autolabor." << endl;
662662

663663
cleanup_state();
664664
init_state();
@@ -975,7 +975,8 @@ static void assign_labor(unit_labor::unit_labor labor,
975975
if (!dwarfs[dwarf]->status.labors[labor])
976976
dwarf_info[dwarf].assigned_jobs++;
977977

978-
dwarfs[dwarf]->status.labors[labor] = true;
978+
if (Units::isValidLabor(dwarfs[dwarf], labor))
979+
dwarfs[dwarf]->status.labors[labor] = true;
979980

980981
if (labor_infos[labor].is_exclusive)
981982
{

plugins/manipulator.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,6 @@ struct SkillColumn
9595
df::job_skill skill; // displayed rating
9696
char label[3]; // column header
9797
bool special; // specified labor is mutually exclusive with all other special labors
98-
bool isValidLabor (df::historical_entity *entity = NULL) const
99-
{
100-
if (labor == unit_labor::NONE)
101-
return false;
102-
if (entity && entity->entity_raw && !entity->entity_raw->jobs.permitted_labor[labor])
103-
return false;
104-
return true;
105-
}
10698
};
10799

108100
#define NUM_COLUMNS (sizeof(columns) / sizeof(SkillColumn))
@@ -1631,9 +1623,10 @@ void viewscreen_unitlaborsst::feed(set<df::interface_key> *events)
16311623
}
16321624

16331625
UnitInfo *cur = units[input_row];
1634-
if (events->count(interface_key::SELECT) && (cur->allowEdit) && columns[input_column].isValidLabor(ui->main.fortress_entity))
1626+
df::unit *unit = cur->unit;
1627+
df::unit_labor cur_labor = columns[input_column].labor;
1628+
if (events->count(interface_key::SELECT) && (cur->allowEdit) && Units::isValidLabor(unit, cur_labor))
16351629
{
1636-
df::unit *unit = cur->unit;
16371630
const SkillColumn &col = columns[input_column];
16381631
bool newstatus = !unit->status.labors[col.labor];
16391632
if (col.special)
@@ -1650,16 +1643,15 @@ void viewscreen_unitlaborsst::feed(set<df::interface_key> *events)
16501643
}
16511644
unit->status.labors[col.labor] = newstatus;
16521645
}
1653-
if (events->count(interface_key::SELECT_ALL) && (cur->allowEdit) && columns[input_column].isValidLabor(ui->main.fortress_entity))
1646+
if (events->count(interface_key::SELECT_ALL) && (cur->allowEdit) && Units::isValidLabor(unit, cur_labor))
16541647
{
1655-
df::unit *unit = cur->unit;
16561648
const SkillColumn &col = columns[input_column];
16571649
bool newstatus = !unit->status.labors[col.labor];
16581650
for (int i = 0; i < NUM_COLUMNS; i++)
16591651
{
16601652
if (columns[i].group != col.group)
16611653
continue;
1662-
if (!columns[i].isValidLabor(ui->main.fortress_entity))
1654+
if (!Units::isValidLabor(unit, columns[i].labor))
16631655
continue;
16641656
if (columns[i].special)
16651657
{
@@ -2055,7 +2047,7 @@ void viewscreen_unitlaborsst::render()
20552047

20562048
}
20572049

2058-
canToggle = (cur->allowEdit) && columns[sel_column].isValidLabor(ui->main.fortress_entity);
2050+
canToggle = (cur->allowEdit) && Units::isValidLabor(unit, columns[sel_column].labor);
20592051
}
20602052

20612053
int x = 2, y = dim.y - 4;

0 commit comments

Comments
 (0)