// Generated by STruC++ - IEC 61131-3 Structured Text to C++ Compiler
// Do not edit this file manually.

#include "generated.hpp"

namespace strucpp {

FB_DUTY_SELECT::FB_DUTY_SELECT()
    : SERVICE_PENALTY(1000000.0)
{
    // Initialize variables
}

void FB_DUTY_SELECT::operator()() {
    for (I = 1; I <= 3; I++) {
        USED.at(I) = false;
        SEL.at(I) = false;
        RANK.at(I) = 0;
    }
    NRANKED = 0;
    for (K = 1; K <= 3; K++) {
        BEST = 0;
        BESTKEY = 0.0;
        for (I = 1; I <= 3; I++) {
            if ((AVAILABLE.at(I)) & (!USED.at(I))) {
                KEY = RUNHOURS.at(I);
                if (SERVICEDUE.at(I)) {
                    KEY = KEY + SERVICE_PENALTY;
                }
                if (((BEST == 0)) | ((KEY < BESTKEY))) {
                    BEST = I;
                    BESTKEY = KEY;
                }
            }
        }
        if (BEST > 0) {
            NRANKED = NRANKED + 1;
            RANK.at(NRANKED) = BEST;
            USED.at(BEST) = true;
        }
    }
    SLOTS = PUMPSREQUIRED;
    if (SLOTS < 0) {
        SLOTS = 0;
    }
    if (SLOTS > 3) {
        SLOTS = 3;
    }
    CNT = 0;
    for (K = 1; K <= NRANKED; K++) {
        I = RANK.at(K);
        if ((RUNNINGNOW.at(I)) & ((CNT < SLOTS))) {
            SEL.at(I) = true;
            CNT = CNT + 1;
        }
    }
    for (K = 1; K <= NRANKED; K++) {
        I = RANK.at(K);
        if (((!SEL.at(I))) & ((CNT < SLOTS))) {
            SEL.at(I) = true;
            CNT = CNT + 1;
        }
    }
    DUTYPUMP = 0;
    for (K = 1; K <= NRANKED; K++) {
        I = RANK.at(K);
        if ((SEL.at(I)) & ((DUTYPUMP == 0))) {
            DUTYPUMP = I;
        }
    }
    for (I = 1; I <= 3; I++) {
        RUNREQUEST.at(I) = SEL.at(I);
    }
}

}  // namespace strucpp