(version 2)
(include "sci.sh")
(use "Cycle")
(script 16)



(class OccasionalCycle of Cycle
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
        lowerEnd 5
        upperEnd 6
        lowCycles 1
        upperCycles 1
        numCycles 1
        waitCycles 0
    )

    (method (init theClient theCycleDir theLowerEnd theUpperEnd theLowCycles theUpperCycles)
        (if (paramTotal)
            = client theClient
            (if (>= paramTotal 2)
                = cycleDir theCycleDir
                (if (>= paramTotal 3)
                    = lowerEnd theLowerEnd
                    (if (>= paramTotal 4)
                        = upperEnd theUpperEnd
                        (if (>= paramTotal 5)
                            = lowCycles theLowCycles
                            (if (>= paramTotal 6)
                                = upperCycles theUpperCycles
                            )
                        )
                    )
                )
            )
        )
        = completed (= cycleCnt 0)
        (self:cycleDone())
    )


    (method (doit)
        (var temp0)
        (asm
            pushi   #nextCel
            pushi   0
            self    4
            sat     temp0
            pToa    waitCycles
            bnt     code_009c
            dpToa   waitCycles
            jmp     code_00d6
code_009c:  lst     temp0
            pushi   #lastCel
            pushi   0
            pToa    client
            send    4
            gt?     
            bt      code_00b0
            lst     temp0
            ldi     0
            lt?     
            bnt     code_00cd
code_00b0:  jmp     code_00b2
code_00b2:  bnt     code_00cd
            pToa    numCycles
            bnt     code_00c5
            dpToa   numCycles
            pushi   #cel
            pushi   1
            lst     temp0
            pToa    client
            send    6
            jmp     code_00d6
code_00c5:  pushi   #cycleDone
            pushi   0
            self    4
            jmp     code_00d6
code_00cd:  pushi   #cel
            pushi   1
            lst     temp0
            pToa    client
            send    6
code_00d6:  ret     
        )
    )


    (method (cycleDone)
        = waitCycles Random(lowerEnd upperEnd)
        = numCycles Random(lowCycles upperCycles)
        (send client:cel(
            (if (= cycleDir 1)
                0
            )(else
                (send client:lastCel())
            )
))
        = cycleCnt GetTime()
    )

)
