(version 2)
(include "sci.sh")
(use "Main")
(use "n982")
(use "Cycle")
(use "Obj")
(script 977)


(local
    local0[8] = (2 6 4 0 3 5 1 7 )
    local8[8] = (3 6 0 4 2 5 1 7 )

)
(class Grooper of Code
    (properties
        client 0
        oldCycler 0
        oldMover 0
        caller 0
    )

    (method (doit theClient param2 theCaller param4)
        (var temp0, temp1)
        (if (not client)
            = client theClient
        )
        (if (& (send client:signal) $0800)
            (if (caller)
                (send caller:cue())
            )
            = caller 0
            return 
        )
        (if (>= paramTotal 3)
            = caller theCaller
        )
        (if (not (send gCast:contains(client)) or ((>= paramTotal 4) and param4))
            = temp1 NumLoops(client)
            (send client:loop((aLA:doit(1 (* 
                    (if (== temp1 4)
                        2
                    )(else
                        1
                    )
 (/ proc999_1((+ (send client:heading) (/ 180 temp1)) 360) (/ 360 temp1)))))))
            (if (caller)
                (send caller:cue())
            )
            = caller 0
            return 
        )
        = temp0 (aLA:doit(0 (send client:loop)))
        = temp1 
            (if (< NumLoops(client) 8)
                4
            )(else
                8
            )
        (if (oldMover)
            (send oldMover:dispose())
            = oldMover 0
        )
        (if (< Abs((- (* 45 temp0) param2)) (+ (/ 180 temp1) 1))
            (if (caller)
                (send caller:cue())
            )
            = caller 0
            return 
        )
        (if (IsObject(oldCycler) and ((send oldCycler:isMemberOf(Grycler)) or not (send ((send client:cycler)):isMemberOf(Grycler))))
            (send oldCycler:dispose())
            = oldCycler 0
        )
        (if (not oldCycler)
            = oldCycler (send client:cycler)
        )
        (if ((send client:cycler) and (send ((send client:cycler)):isMemberOf(Grycler)))
            (send ((send client:cycler)):dispose())
        )
        = oldMover (send client:mover)
        (send client:
            cycler(0)
            mover(0)
            setMotion(0)
            setCycle(Grycler self temp0)
        )
    )


    (method (dispose)
        (if (IsObject(oldCycler))
            (send oldCycler:dispose())
            = oldCycler 0
        )
        (if (IsObject(oldMover))
            (send oldMover:dispose())
            = oldMover 0
        )
        (if (client)
            (send client:looper(0))
        )
        (super:dispose())
    )


    (method (cue param1)
        (var theCaller)
        (if (not IsObject((send client:mover)))
            (send client:mover(oldMover))
        )
        (if (IsObject(oldCycler))
            (send client:cycler(oldCycler))
        )
        = theCaller caller
        = caller (= oldMover (= oldCycler 0))
        (if (theCaller)
            (send theCaller:cue(rest param1))
        )
    )

)
(class Grycler of Cycle
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
        loopIndex 0
        numOfLoops 0
    )

    (method (init param1 theCaller theLoopIndex)
        (super:init(param1))
        = caller theCaller
        = numOfLoops NumLoops(client)
        = cycleDir neg proc999_0(proc982_2((* theLoopIndex 45) (send param1:heading)))
        = loopIndex theLoopIndex
    )


    (method (doit)
        (if (>= Abs(proc982_2((* loopIndex 45) (send client:heading))) (+ (/ 180 numOfLoops) 1))
            (send client:loop((self:nextCel())))
        )(else
            (self:cycleDone())
        )
    )


    (method (nextCel)
        ++cycleCnt
        (if (<= cycleCnt (send client:cycleSpeed))
            (send client:loop)
        )(else
            = cycleCnt 0
            = loopIndex (+ loopIndex (* cycleDir (/ 8 numOfLoops)))
            = loopIndex proc999_1(loopIndex 8)
            (aLA:doit(1 loopIndex))
        )
    )


    (method (cycleDone)
        = global58 (= completed 1)
    )

)
(instance aLA of Code
    (properties)

    (method (doit param1 param2)
        return 
            (if (== param1 0)
                local0[param2]
            )(else
                local8[param2]
            )
    )

)
