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

(class IncrementalEnd of End
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
        endCel 0
        maxLoops 0
        lastView 0
        curLoop 0
        curView 0
        origView 0
        origLoop 0
        origCel 0
    )

    (method (init theClient theLastView param3)
        = client theClient
        = origCel (send client:cel)
        = origView (send client:view)
        = origLoop (send client:loop)
        = curView (send client:view)
        = maxLoops NumLoops(client)
        --maxLoops
        (if (>= paramTotal 2)
            = lastView theLastView
        )(else
            = lastView (send client:view)
        )
        (super:init(theClient theClient rest param3))
    )


    (method (doit)
        (var incrementalEndNextCel)
        = incrementalEndNextCel (self:nextCel())
        (send client:cel(incrementalEndNextCel))
        (if (== (+ (send client:lastCel()) 1) incrementalEndNextCel)
            (if (<= ++curLoop maxLoops)
                (send client:
                    setLoop(curLoop)
                    cel(0)
                )
            )(else
                (if (<= ++curView lastView)
                    = curLoop 0
                    (send client:
                        view(curView)
                        setLoop(0)
                        cel(0)
                    )
                    = maxLoops NumLoops(client)
                    --maxLoops
                )(else
                    (send client:cel((send client:lastCel())))
                    (self:cycleDone())
                )
            )
        )
    )


    (method (cycleDone)
        (send global1:handsOn())
        (send client:
            view(origView)
            loop(origLoop)
            cel(origCel)
        )
        (super:cycleDone())
    )

)
