Add isValidLabor() to Units module

Closes #711 and could avoid potential issues when editing units from
other civs with manipulator.
develop
lethosor 2015-10-30 18:41:09 -04:00
parent ff4f7203c5
commit ac71fa8070
6 changed files with 27 additions and 16 deletions

@ -1519,6 +1519,7 @@ static const LuaWrapper::FunctionReg dfhack_units_module[] = {
WRAPM(Units, getNominalSkill), WRAPM(Units, getNominalSkill),
WRAPM(Units, getEffectiveSkill), WRAPM(Units, getEffectiveSkill),
WRAPM(Units, getExperience), WRAPM(Units, getExperience),
WRAPM(Units, isValidLabor),
WRAPM(Units, computeMovementSpeed), WRAPM(Units, computeMovementSpeed),
WRAPM(Units, computeSlowdownFactor), WRAPM(Units, computeSlowdownFactor),
WRAPM(Units, getProfessionName), WRAPM(Units, getProfessionName),

@ -269,6 +269,8 @@ DFHACK_EXPORT int getNominalSkill(df::unit *unit, df::job_skill skill_id, bool u
DFHACK_EXPORT int getEffectiveSkill(df::unit *unit, df::job_skill skill_id); DFHACK_EXPORT int getEffectiveSkill(df::unit *unit, df::job_skill skill_id);
DFHACK_EXPORT int getExperience(df::unit *unit, df::job_skill skill_id, bool total = false); DFHACK_EXPORT int getExperience(df::unit *unit, df::job_skill skill_id, bool total = false);
DFHACK_EXPORT bool isValidLabor(df::unit *unit, df::unit_labor labor);
DFHACK_EXPORT int computeMovementSpeed(df::unit *unit); DFHACK_EXPORT int computeMovementSpeed(df::unit *unit);
DFHACK_EXPORT float computeSlowdownFactor(df::unit *unit); DFHACK_EXPORT float computeSlowdownFactor(df::unit *unit);

@ -1223,6 +1223,19 @@ int Units::getEffectiveSkill(df::unit *unit, df::job_skill skill_id)
return rating; return rating;
} }
bool Units::isValidLabor(df::unit *unit, df::unit_labor labor)
{
CHECK_NULL_POINTER(unit);
if (!is_valid_enum_item(labor))
return false;
if (labor == df::unit_labor::NONE)
return false;
df::historical_entity *entity = df::historical_entity::find(unit->civ_id);
if (entity && entity->entity_raw && !entity->entity_raw->jobs.permitted_labor[labor])
return false;
return true;
}
inline void adjust_speed_rating(int &rating, bool is_adventure, int value, int dwarf100, int dwarf200, int adv50, int adv75, int adv100, int adv200) inline void adjust_speed_rating(int &rating, bool is_adventure, int value, int dwarf100, int dwarf200, int adv50, int adv75, int adv100, int adv200)
{ {
if (is_adventure) if (is_adventure)

@ -951,6 +951,8 @@ 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++)
{ {
if (!Units::isValidLabor(dwarfs[dwarf], labor))
continue;
// Set hauling labors based on employment states // Set hauling labors based on employment states
if(dwarf_info[dwarf].state == IDLE) { if(dwarf_info[dwarf].state == IDLE) {

@ -658,7 +658,7 @@ static void enable_plugin(color_ostream &out)
setOptionEnabled(CF_ENABLED, true); setOptionEnabled(CF_ENABLED, true);
enable_autolabor = true; enable_autolabor = true;
out << "Enabling the plugin." << endl; out << "Enabling autolabor." << endl;
cleanup_state(); cleanup_state();
init_state(); init_state();
@ -975,6 +975,7 @@ static void assign_labor(unit_labor::unit_labor labor,
if (!dwarfs[dwarf]->status.labors[labor]) if (!dwarfs[dwarf]->status.labors[labor])
dwarf_info[dwarf].assigned_jobs++; dwarf_info[dwarf].assigned_jobs++;
if (Units::isValidLabor(dwarfs[dwarf], labor))
dwarfs[dwarf]->status.labors[labor] = true; dwarfs[dwarf]->status.labors[labor] = true;
if (labor_infos[labor].is_exclusive) if (labor_infos[labor].is_exclusive)

@ -95,14 +95,6 @@ struct SkillColumn
df::job_skill skill; // displayed rating df::job_skill skill; // displayed rating
char label[3]; // column header char label[3]; // column header
bool special; // specified labor is mutually exclusive with all other special labors bool special; // specified labor is mutually exclusive with all other special labors
bool isValidLabor (df::historical_entity *entity = NULL) const
{
if (labor == unit_labor::NONE)
return false;
if (entity && entity->entity_raw && !entity->entity_raw->jobs.permitted_labor[labor])
return false;
return true;
}
}; };
#define NUM_COLUMNS (sizeof(columns) / sizeof(SkillColumn)) #define NUM_COLUMNS (sizeof(columns) / sizeof(SkillColumn))
@ -1631,9 +1623,10 @@ void viewscreen_unitlaborsst::feed(set<df::interface_key> *events)
} }
UnitInfo *cur = units[input_row]; UnitInfo *cur = units[input_row];
if (events->count(interface_key::SELECT) && (cur->allowEdit) && columns[input_column].isValidLabor(ui->main.fortress_entity))
{
df::unit *unit = cur->unit; df::unit *unit = cur->unit;
df::unit_labor cur_labor = columns[input_column].labor;
if (events->count(interface_key::SELECT) && (cur->allowEdit) && Units::isValidLabor(unit, cur_labor))
{
const SkillColumn &col = columns[input_column]; const SkillColumn &col = columns[input_column];
bool newstatus = !unit->status.labors[col.labor]; bool newstatus = !unit->status.labors[col.labor];
if (col.special) if (col.special)
@ -1650,16 +1643,15 @@ void viewscreen_unitlaborsst::feed(set<df::interface_key> *events)
} }
unit->status.labors[col.labor] = newstatus; unit->status.labors[col.labor] = newstatus;
} }
if (events->count(interface_key::SELECT_ALL) && (cur->allowEdit) && columns[input_column].isValidLabor(ui->main.fortress_entity)) if (events->count(interface_key::SELECT_ALL) && (cur->allowEdit) && Units::isValidLabor(unit, cur_labor))
{ {
df::unit *unit = cur->unit;
const SkillColumn &col = columns[input_column]; const SkillColumn &col = columns[input_column];
bool newstatus = !unit->status.labors[col.labor]; bool newstatus = !unit->status.labors[col.labor];
for (int i = 0; i < NUM_COLUMNS; i++) for (int i = 0; i < NUM_COLUMNS; i++)
{ {
if (columns[i].group != col.group) if (columns[i].group != col.group)
continue; continue;
if (!columns[i].isValidLabor(ui->main.fortress_entity)) if (!Units::isValidLabor(unit, columns[i].labor))
continue; continue;
if (columns[i].special) if (columns[i].special)
{ {
@ -2055,7 +2047,7 @@ void viewscreen_unitlaborsst::render()
} }
canToggle = (cur->allowEdit) && columns[sel_column].isValidLabor(ui->main.fortress_entity); canToggle = (cur->allowEdit) && Units::isValidLabor(unit, columns[sel_column].labor);
} }
int x = 2, y = dim.y - 4; int x = 2, y = dim.y - 4;