Factor out set and add shared action timer code into own functions

develop
Tachytaenius 2022-11-23 13:17:32 +00:00
parent 357b871b4f
commit e93d7eefb3
1 changed files with 19 additions and 16 deletions

@ -2008,16 +2008,21 @@ int32_t *getActionTimerPointer(df::unit_action *action) {
return nullptr; return nullptr;
} }
void Units::subtractActionTimer(df::unit *unit, int32_t amount, df::unit_action_type affectedActionType) void subtractActionTimerCore(df::unit_action *action, int32_t amount)
{ {
CHECK_NULL_POINTER(unit);
for (auto action : unit->actions) {
if (affectedActionType != action->type) continue;
int32_t *timer = getActionTimerPointer(action); int32_t *timer = getActionTimerPointer(action);
if (timer != nullptr && *timer != 0) { if (timer != nullptr && *timer != 0) {
*timer = max(*timer - amount, 1); *timer = max(*timer - amount, 1);
} }
} }
void Units::subtractActionTimer(df::unit *unit, int32_t amount, df::unit_action_type affectedActionType)
{
CHECK_NULL_POINTER(unit);
for (auto action : unit->actions) {
if (affectedActionType != action->type) continue;
subtractActionTimerCore(action, amount);
}
} }
void Units::subtractCategoryActionTimers(df::unit *unit, int32_t amount, df::unit_action_type_group affectedActionTypes) void Units::subtractCategoryActionTimers(df::unit *unit, int32_t amount, df::unit_action_type_group affectedActionTypes)
@ -2027,10 +2032,7 @@ void Units::subtractCategoryActionTimers(df::unit *unit, int32_t amount, df::uni
auto list = ENUM_ATTR(unit_action_type, group, action->type); auto list = ENUM_ATTR(unit_action_type, group, action->type);
for (size_t i = 0; i < list.size; i++) { for (size_t i = 0; i < list.size; i++) {
if (list.items[i] == affectedActionTypes) { if (list.items[i] == affectedActionTypes) {
int32_t *timer = getActionTimerPointer(action); subtractActionTimerCore(action, amount);
if (timer != nullptr && *timer != 0) {
*timer = max(*timer - amount, 1);
}
break; break;
} }
} }
@ -2073,15 +2075,19 @@ void Units::multiplyCategoryActionTimers(df::unit *unit, float amount, df::unit_
} }
} }
void setActionTimerCore(df::unit_action *action, int32_t amount) {
int32_t *timer = getActionTimerPointer(action);
if (timer != nullptr && *timer != 0) {
*timer = amount;
}
}
void Units::setActionTimer(df::unit *unit, int32_t amount, df::unit_action_type affectedActionType) void Units::setActionTimer(df::unit *unit, int32_t amount, df::unit_action_type affectedActionType)
{ {
CHECK_NULL_POINTER(unit); CHECK_NULL_POINTER(unit);
for (auto action : unit->actions) { for (auto action : unit->actions) {
if (affectedActionType != action->type) continue; if (affectedActionType != action->type) continue;
int32_t *timer = getActionTimerPointer(action); setActionTimerCore(action, amount);
if (timer != nullptr && *timer != 0) {
*timer = amount;
}
} }
} }
@ -2092,10 +2098,7 @@ void Units::setCategoryActionTimers(df::unit *unit, int32_t amount, df::unit_act
auto list = ENUM_ATTR(unit_action_type, group, action->type); auto list = ENUM_ATTR(unit_action_type, group, action->type);
for (size_t i = 0; i < list.size; i++) { for (size_t i = 0; i < list.size; i++) {
if (list.items[i] == affectedActionTypes) { if (list.items[i] == affectedActionTypes) {
int32_t *timer = getActionTimerPointer(action); setActionTimerCore(action, amount);
if (timer != nullptr && *timer != 0) {
*timer = amount;
}
break; break;
} }
} }