(version 2)
(include "sci.sh")
(use "n982")
(use "Obj")
(script 985)



(class Avoid of Obj
    (properties
        client 0
        heading -1000
        bumpTurn 0
        lastBumped 0
        thisTurn 1
        escaping 0
        escapes 0
        escapeTurn 1
        nearestDist 32000
        counter 0
        nonBumps 10
        targetX 0
        targetY 0
        motionInited 0
        outOfTouch 0
        offScreenOK 0
    )

    (method (init theClient theOffScreenOK)
        (if (>= paramTotal 1)
            = client theClient
        )
        (if (>= paramTotal 2)
            = offScreenOK theOffScreenOK
        )
        = heading (send client:heading)
        = counter 0
        = nonBumps 10
        = escaping 0
    )


    (method (doit)
        (var theHeading, theNearestDist, clientX, clientY, theTargetX, theTargetY, clientMover, temp7, temp8, clientMoverB_moveCnt, theClientMoverB_moveCnt, temp11, temp12, temp13, temp14, temp15)
        = temp15 proc999_3(4 (* (/ NumLoops(client) 4) 4))
        = clientMover (send client:mover)
        (if (clientMover)
            (send (= clientMover (send client:mover)):setTarget())
        )
        (if (not clientMover)
            = heading -1000
            return 
        )(else
            = theTargetX (send clientMover:x)
            = theTargetY (send clientMover:y)
            (if (not 
            (if ((self:canBeHere(theTargetY theTargetY)))
            )(else
                (send clientMover:respondsTo(#distance))
            )
)
                (send clientMover:doit())
                return 
            )(else
                (if ((send clientMover:onTarget()))
                    (if (motionInited)
                    )(else
                        InitBresen(clientMover)
                    )
                    (send clientMover:doit())
                    return 
                )(else
                    (if (== heading -1000)
                        (self:init())
                    )
                )
            )
        )
        = temp13 0
        = clientX (send client:x)
        = clientY (send client:y)
        = temp7 proc999_1(GetAngle(clientX clientY theTargetX theTargetY) 360)
        = theNearestDist GetDistance(clientX clientY theTargetX theTargetY)
        = temp14 (/ 180 temp15)
        = theHeading (= heading proc999_1((* temp14 (/ heading temp14)) 360))
        (if (not 
        (if (escaping)
        )(else
            = temp12 (<= nonBumps 2)
        )
)
            (if (not motionInited)
                = motionInited 1
                InitBresen(clientMover)
            )
            (send clientMover:doit())
            (if ((<> clientX (send client:x)) or (<> clientY (send client:y)))
                (self:pickLoop(temp7))
                ++nonBumps
                return 
            )
            = nonBumps 0
            = temp13 1
            = motionInited 0
        )(else
            = clientMoverB_moveCnt (send clientMover:{b-moveCnt})
            (if (< clientMoverB_moveCnt (send client:moveSpeed))
                (send clientMover:{b-moveCnt}(++clientMoverB_moveCnt))
                return 
            )(else
                (send clientMover:{b-moveCnt}(0))
            )
        )
        (if (((<> targetX (= targetX theTargetX)) or (<> targetY (= targetY theTargetY))) or (escaping and ((<= --counter 0) or ((< theNearestDist nearestDist) and (< counter 40)))))
            = motionInited (= escaping 0)
            = nearestDist 32000
            = counter 0
        )(else
            (if (escaping)
            )(else
                (if (< theNearestDist nearestDist)
                    = nearestDist theNearestDist
                    = counter 0
                )(else
                    (if (= escaping (>= ++counter 50))
                        = counter Random(40 80)
                        = escapeTurn neg escapeTurn
                    )
                )
            )
        )
        = temp7 proc982_2(temp7 theHeading)
        = temp8 Abs(temp7)
        = temp11 (<= temp8 90)
        = thisTurn 
            (if (proc999_0(temp7))
            )(else
                escapeTurn
            )
        (if ((> temp8 (/ temp14 2)) and not escaping)
            = heading (+ heading (* thisTurn temp14))
        )
        (if (escaping)
            = thisTurn escapeTurn
        )
        (self:incClientPos())
        (if ((self:canBeHere()))
            ++nonBumps
            = bumpTurn 0
            (if (not escaping)
                return 
            )
        )(else
            = lastBumped bumpTurn
            = bumpTurn thisTurn
            = nonBumps 0
        )
        = temp15 (* temp15 2)
        (if (escaping)
            = theHeading (* temp14 (/ heading temp14))
            = theClientMoverB_moveCnt 0
            (while ((< theClientMoverB_moveCnt temp15) and (self:canBeHere()))
                = heading proc999_1((+ theHeading (* (* temp14 theClientMoverB_moveCnt) escapeTurn)) 360)
                (send client:
                    x(clientX)
                    y(clientY)
                )
                (self:incClientPos())
                ++theClientMoverB_moveCnt
            )
            (if (== theClientMoverB_moveCnt temp15)
                = heading (+ heading (* escapeTurn temp14))
            )
            = theHeading heading
        )
        = clientMoverB_moveCnt 1
        = theClientMoverB_moveCnt clientMoverB_moveCnt
        (while (not (self:canBeHere()) and (< clientMoverB_moveCnt temp15))
            (send client:
                x(clientX)
                y(clientY)
            )
            = heading (- theHeading (* (* temp14 theClientMoverB_moveCnt) thisTurn))
            (self:incClientPos())
            = theClientMoverB_moveCnt 
                (if (escaping)
                    ++clientMoverB_moveCnt
                )(else
                    (if (> theClientMoverB_moveCnt 0)
                        neg theClientMoverB_moveCnt
                    )(else
                        ++clientMoverB_moveCnt
                    )
                )
        )
        (self:pickLoop(heading))
    )


    (method (incClientPos)
        (send client:
            x(+ (send client:x) (* proc999_0(SinMult(heading 100)) (send client:xStep)))
            y(- (send client:y) (* proc999_0(CosMult(heading 100)) (send client:yStep)))
            heading(heading)
        )
    )


    (method (pickLoop param1)
        (send client:heading(param1))
        (if ((send client:looper))
            (send ((send client:looper)):doit(client param1 0 1))
        )(else
            DirLoop(client param1)
        )
    )


    (method (canBeHere param1 param2)
        (var clientX, clientY, temp2)
        = clientX (send client:x)
        = clientY (send client:y)
        (if (paramTotal)
            (send client:
                x(param1)
                y(param2)
            )
        )
        = temp2 
            (if (not (send client:canBeHere()) or offScreenOK)
            )(else
                not proc982_0(client)
            )
        (send client:
            x(clientX)
            y(clientY)
        )
        return temp2
    )

)
