(include "sci.sh")
(use "Main")
(use "Obj")
(script 992)



(class Cycle of Obj
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
    )

    (method (init theClient)
        (if (paramTotal)
            = client theClient
        )
        = completed (= cycleCnt 0)
    )


    (method (nextCel)
        ++cycleCnt
        return 
            (if (<= cycleCnt (send client:cycleSpeed))
                (send client:cel)
            )(else
                = cycleCnt 0
                (if (& (send client:signal) $1000)
                    (send client:cel)
                )(else
                    + (send client:cel) cycleDir
                )
            )
    )


    (method (cycleDone)
    )


    (method (motionCue)
        (send client:cycler(0))
        (if (completed and IsObject(caller))
            (send caller:cue())
        )
        (self:dispose())
    )

)
(class Fwd of Cycle
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
    )

    (method (doit)
        (var fwdNextCel)
        = fwdNextCel (self:nextCel())
        (if (> fwdNextCel (send client:lastCel()))
            (self:cycleDone())
        )(else
            (send client:cel(fwdNextCel))
        )
    )


    (method (cycleDone)
        (send client:cel(0))
    )

)
(class Walk of Fwd
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
    )

    (method (doit)
        (var temp0)
        (if (not (send client:isStopped()))
            (super:doit())
        )
    )

)
(class CT of Cycle
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
        endCel 0
    )

    (method (init param1 param2 theCycleDir theCaller)
        (var clientLastCel)
        (super:init(param1))
        = cycleDir theCycleDir
        (if (== paramTotal 4)
            = caller theCaller
        )
        = clientLastCel (send client:lastCel())
        = endCel 
            (if (> param2 (= clientLastCel (send client:lastCel())))
                clientLastCel
            )(else
                param2
            )
    )


    (method (doit)
        (var temp0, clientLastCel)
        = clientLastCel (send client:lastCel())
        (send (
        (if (> endCel (= clientLastCel (send client:lastCel())))
            = endCel clientLastCel
        )(else
            0
        )
):)
        (if ((== cycleCnt 0) and (== endCel (send client:cel)))
            (self:cycleDone())
        )
    )


    (method (cycleDone)
        = completed 1
        (if (caller)
            = global37 1
        )(else
            (self:motionCue())
        )
    )

)
(class End of CT
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
        endCel 0
    )

    (method (init param1 param2)
        (super:init(param1 (send param1:lastCel()) 1 
            (if (== paramTotal 2)
                param2
            )(else
                0
            )
))
    )

)
(class Beg of CT
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
        endCel 0
    )

    (method (init param1 param2)
        (super:init(param1 0 -1 
            (if (== paramTotal 2)
                param2
            )(else
                0
            )
))
    )

)
(class SyncWalk of Fwd
    (properties
        client 0
        caller 0
        cycleDir 1
        cycleCnt 0
        completed 0
        xLast 0
        yLast 0
    )

    (method (doit)
        (var clientMover)
        = clientMover (send client:mover)
        (if (IsObject(clientMover) and ((<> (send client:x) xLast) or (<> (send client:y) yLast)))
            = xLast (send client:x)
            = yLast (send client:y)
            (super:doit())
        )
    )


    (method (nextCel)
        = cycleCnt (send client:cycleSpeed)
        (super:nextCel())
    )

)
(class Motion of Obj
    (properties
        client 0
        caller 0
        x 0
        y 0
        dx 0
        dy 0
        {b-moveCnt} 0
        {b-i1} 0
        {b-i2} 0
        {b-di} 0
        {b-xAxis} 0
        {b-incr} 0
        completed 0
        xLast 0
        yLast 0
    )

    (method (init param1 param2 param3 param4)
        (var temp0[2], temp2, temp3)
        (asm
            lsp     paramTotal
            ldi     1
            ge?     
            bnt     code_0412
            lap     param1
            aTop    client
            lsp     paramTotal
            ldi     2
            ge?     
            bnt     code_0412
            lap     param2
            aTop    x
            lsp     paramTotal
            ldi     3
            ge?     
            bnt     code_0412
            lap     param3
            aTop    y
            lsp     paramTotal
            ldi     4
            ge?     
            bnt     code_0412
            lap     param4
            aTop    caller
code_0412:  ldi     0
            aTop    completed
            aTop    {b-moveCnt}
            aTop    xLast
            aTop    yLast
            pushi   #cycler
            pushi   0
            pToa    client
            send    4
            sat     temp3
            bnt     code_0430
            pushi   #cycleCnt
            pushi   1
            pushi   0
            send    6
code_0430:  pushi   4
            dup     
            pushi   0
            pToa    client
            send    4
            sat     temp2
            push    
            pushi   #y
            pushi   0
            pToa    client
            send    4
            sat     temp3
            push    
            pTos    x
            pTos    y
            callk   GetDistance, 8
            bnt     code_0465
            pushi   #setHeading
            pushi   1
            pushi   4
            lst     temp2
            lst     temp3
            pTos    x
            pTos    y
            callk   GetAngle, 8
            push    
            pToa    client
            send    6
code_0465:  pushi   1
            pushSelf
            callk   InitBresen, 2
            ret     
        )
    )


    (method (doit)
        (var temp0[6])
        DoBresen(self)
    )


    (method (moveDone)
        = completed 1
        (if (caller)
            = global37 1
        )(else
            (self:motionCue())
        )
    )


    (method (setTarget theX theY)
        (if (paramTotal)
            = x theX
            = y theY
        )
    )


    (method (onTarget)
        return 
            (if (== (send client:x) x)
                == (send client:y) y
            )(else
                0
            )
    )


    (method (motionCue)
        (send client:mover(0))
        (if (completed and IsObject(caller))
            (send caller:cue())
        )
        (self:dispose())
    )

)
(class MoveTo of Motion
    (properties
        client 0
        caller 0
        x 0
        y 0
        dx 0
        dy 0
        {b-moveCnt} 0
        {b-i1} 0
        {b-i2} 0
        {b-di} 0
        {b-xAxis} 0
        {b-incr} 0
        completed 0
        xLast 0
        yLast 0
    )

    (method (init param1)
        (super:init(rest param1))
    )


    (method (onTarget)
        return 
            (if (<= Abs((- (send client:x) x)) (send client:xStep))
                <= Abs((- (send client:y) y)) (send client:yStep)
            )(else
                0
            )
    )

)
