Skip to content

File firecontrol.cpp

File List > firecontrol.cpp

Go to the documentation of this file

#include "firecontrol.h"

#include <cstdint>
#include <functional>

#include "utilities.h"

FireControl::FireControl(bool initial_active, uint16_t initial_duration, int64_t initial_run_after):
    Command(static_cast<uint64_t>(initial_run_after)), active(initial_active), duration(initial_duration) {}

void FireControl::Execute(SystemState* state) {
    // Do nothing if the desired fire state is already the current state
    if (state->getFireState() == active) {
        return;
    }

    Target* target = state->currentTarget();
    auto    actionable = target->actionable();

    // Activate firing if the target has been idle longer than the action interval
    if (active && actionable) {
        state->setFire(active);
        state->queueCeaseFire(duration);
    }
    // Deactivate firing after the specified duration and increment the action counter
    else if (!active && target->actionIdleExceeds(milliseconds(duration))) {
        state->clearFire(active);
        target->IncrementAction();
    }
}