(version 2)
(include "sci.sh")
(exports
    0 regDune
    1 DUNELOOPER
    2 proc501_2
    3 proc501_3
)
(use "Main")
(use "Class_255_0")
(use "Chase")
(use "Avoid")
(use "Sound")
(use "Cycle")
(use "Game")
(use "User")
(use "Feature")
(use "Obj")
(script 501)


(local
    local0
    local1
    local2
    local3
    newSound
    local5
    local6
    local7[12]

)
(procedure public (proc501_2 param1)
    (var temp0)
    = temp0 OnControl(2 (send gEgo:x) (send gEgo:y))
    return 
        (if (& temp0 param1)
            return 1
        )(else
            return 0
        )
)


(procedure public (proc501_3 param1)
    (var temp0)
    = temp0 0
    (while (param1)
        = param1 (>> param1 $0001)
        ++temp0
    )
    return - temp0 1
)


(class AView of View
    (properties
        y 0
        x 0
        z 0
        heading 0
        yStep 2
        view 0
        loop 0
        cel 0
        priority 0
        underBits 0
        signal 257
        nsTop 0
        nsLeft 0
        nsBottom 0
        nsRight 0
        lsTop 0
        lsLeft 0
        lsBottom 0
        lsRight 0
        brTop 0
        brLeft 0
        brBottom 0
        brRight 0
    )

    (method (delete)
        = signal (& signal $ffdf)
        (super:delete())
    )

)
(instance footPrint of AView
    (properties)
)
(class dMoveTo 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 theClient theX theY theCaller)
        (var temp0[2])
        = client theClient
        = x theX
        (if (<> (send gEgo:looper) 0)
            = y (+ (send gEgo:y) (- (send gEgo:y) theY))
        )(else
            = y theY
        )
        (if (== paramTotal 4)
            = caller theCaller
        )
        = {b-moveCnt} 0
        (send theClient:heading(GetAngle((send theClient:x) (send theClient:y) x y)))
        (if (== global104 1)
            (if ((< (send gEgo:heading) 45) or (> (send gEgo:heading) 315))
                (send gEgo:loop(2))
            )(else
                (if ((>= (send gEgo:heading) 45) and (< (send gEgo:heading) 135))
                    (send gEgo:loop(0))
                )(else
                    (if ((>= (send gEgo:heading) 135) and (< (send gEgo:heading) 225))
                        (send gEgo:loop(3))
                    )(else
                        (send gEgo:loop(1))
                    )
                )
            )
        )(else
            DirLoop(theClient (send theClient:heading))
        )
        InitBresen(self)
    )

)
(instance public regDune of Rgn
    (properties)

    (method (init)
        Load(128 777)
        Load(128 82)
        Load(128 0)
        Load(128 68)
        Load(128 106)
        Load(128 108)
        (User:mapKeyToDir(0))
        (super:init())
    )


    (method (dispose)
        (User:mapKeyToDir(1))
        (super:dispose())
    )


    (method (handleEvent pEvent)
        (var pEventType, gEgoX, gEgoY, temp3, temp4, pEventMessage)
        (if ((send pEvent:claimed))
            return 1
        )
        return 
            (if (== (User:controls) 1)
                = pEventType (send pEvent:type)
                MapKeyToDir(pEvent)
                (switch ((send pEvent:type))
                    (case 1
                        (if ((send global2:controls) and IsObject(gEgo))
                            = gGEgoY (send pEvent:y)
                            = gGEgoX (send pEvent:x)
                            = gGEgoY_5 (send gEgo:y)
                            (send gEgo:setMotion(dMoveTo (send pEvent:x) (send pEvent:y)))
                            (User:prevDir(0))
                            (send pEvent:claimed(1))
                        )
                    )
                    (case 64
                        (if ((send global2:controls) and IsObject(gEgo))
                            = pEventMessage (send pEvent:message)
                            = gGEgoX 0
                            = gGEgoY 0
                            (if (((== pEventType 4) and (== gPEventMessage pEventMessage)) and IsObject((send gEgo:mover)))
                                = pEventMessage 0
                            )
                            = gPEventMessage pEventMessage
                            (if (== pEventMessage 0)
                                (send gEgo:setMotion(0))
                                return (send pEvent:claimed(1))
                            )
                            (if ((send gEgo:mover))
                                (if (== pEventMessage 
                                (if (((== global104 0) or (== global104 4)) or (== global104 2))
                                    (if (== (send gEgo:heading) 0)
                                        1
                                    )(else
                                        (if (< (send gEgo:heading) 90)
                                            2
                                        )(else
                                            (if (== (send gEgo:heading) 90)
                                                3
                                            )(else
                                                (if (< (send gEgo:heading) 180)
                                                    4
                                                )(else
                                                    (if (== (send gEgo:heading) 180)
                                                        5
                                                    )(else
                                                        (if (< (send gEgo:heading) 270)
                                                            6
                                                        )(else
                                                            (if (== (send gEgo:heading) 270)
                                                                7
                                                            )(else
                                                                8
                                                            )
                                                        )
                                                    )
                                                )
                                            )
                                        )
                                    )
                                )(else
                                    (if (== (send gEgo:heading) 180)
                                        1
                                    )(else
                                        (if (< (send gEgo:heading) 90)
                                            4
                                        )(else
                                            (if (== (send gEgo:heading) 90)
                                                3
                                            )(else
                                                (if (< (send gEgo:heading) 180)
                                                    2
                                                )(else
                                                    (if (== (send gEgo:heading) 0)
                                                        5
                                                    )(else
                                                        (if (< (send gEgo:heading) 270)
                                                            8
                                                        )(else
                                                            (if (== (send gEgo:heading) 270)
                                                                7
                                                            )(else
                                                                6
                                                            )
                                                        )
                                                    )
                                                )
                                            )
                                        )
                                    )
                                )
)
                                    return (send pEvent:claimed(1))
                                )
                            )
                            = gEgoX (send gEgo:x)
                            = gEgoY (send gEgo:y)
                            = temp3 (* (send gEgo:xStep) 400)
                            = temp4 (* (send gEgo:yStep) 400)
                            (if (((<> global104 0) and (<> global104 4)) and (<> global104 2))
                                = temp4 (* temp4 -1)
                            )
                            (switch (pEventMessage)
                                (case 1
                                    = gEgoY (- gEgoY temp4)
                                )
                                (case 2
                                    = gEgoX (+ gEgoX temp3)
                                    = gEgoY (- gEgoY temp4)
                                )
                                (case 3
                                    = gEgoX (+ gEgoX temp3)
                                )
                                (case 4
                                    = gEgoX (+ gEgoX temp3)
                                    = gEgoY (+ gEgoY temp4)
                                )
                                (case 5
                                    = gEgoY (+ gEgoY temp4)
                                )
                                (case 6
                                    = gEgoX (- gEgoX temp3)
                                    = gEgoY (+ gEgoY temp4)
                                )
                                (case 7
                                    = gEgoX (- gEgoX temp3)
                                )
                                (case 8
                                    = gEgoX (- gEgoX temp3)
                                    = gEgoY (- gEgoY temp4)
                                )
                            )
                            (send gEgo:setMotion(MoveTo gEgoX gEgoY))
                            (send pEvent:claimed(1))
                        )
                    )
                    (case 128
                        (if (Said('look>'))
                            (if (Said('/desert'))
                                proc255_0(501 0)
                            )(else
                                (if (Said('/dirt,dirt'))
                                    proc255_0(501 1)
                                )(else
                                    (if (Said('/dune'))
                                        proc255_0(501 2)
                                    )(else
                                        (if (Said('/dirt') or Said('<down'))
                                            proc255_0(501 3)
                                        )(else
                                            (if (Said('/lightning'))
                                                proc255_0(501 4)
                                            )(else
                                                (if (Said('/cloud,air') or Said('<up'))
                                                    proc255_0(501 5)
                                                )(else
                                                    (if (Said('/down'))
                                                        proc255_0(501 6)
                                                    )(else
                                                        (if (Said('/bug'))
                                                            (if ((send global5:contains(local6)))
                                                                proc255_0(501 7)
                                                            )(else
                                                                proc255_0(501 8)
                                                            )
                                                        )(else
                                                            (if (Said('/butte,toe'))
                                                                proc255_0(501 9)
                                                            )(else
                                                                (if (Said('/footprint'))
                                                                    (if ((send global5:contains(gNewAct)))
                                                                        proc255_0(501 10)
                                                                    )(else
                                                                        proc255_0(501 11)
                                                                    )
                                                                )(else
                                                                    (if (Said('/rock'))
                                                                        proc255_0(501 12)
                                                                    )(else
                                                                        (if (Said('/cloud'))
                                                                            proc255_0(501 13)
                                                                        )(else
                                                                            (if (Said('/butte'))
                                                                                proc255_0(501 14)
                                                                            )
                                                                        )
                                                                    )
                                                                )
                                                            )
                                                        )
                                                    )
                                                )
                                            )
                                        )
                                    )
                                )
                            )
                        )(else
                            (if (Said('get>'))
                                (if (Said('/dirt'))
                                    proc255_0(501 15)
                                )(else
                                    (if (Said('/rock'))
                                        proc255_0(501 16)
                                    )(else
                                        (if (Said('/bug'))
                                            (if ((send global5:contains(local6)))
                                                proc255_0(501 17)
                                            )(else
                                                proc255_0(501 8)
                                            )
                                        )
                                    )
                                )
                            )(else
                                (if (Said('conceal'))
                                    proc255_0(501 18)
                                )(else
                                    (if (Said('dig'))
                                        proc255_0(501 19)
                                    )(else
                                        (if (Said('attack/bug') or Said('stair/bug'))
                                            (if ((send global5:contains(local6)))
                                                proc255_0(501 20)
                                            )
                                        )(else
                                            (if (Said('climb,sit,crawl,lie'))
                                                proc255_0(501 21)
                                            )(else
                                                (if (Said('converse/android'))
                                                    (if ((send global5:contains(gNewAct)))
                                                        proc255_0(501 22)
                                                    )(else
                                                        proc255_0(501 23)
                                                    )
                                                )
                                            )
                                        )
                                    )
                                )
                            )
                        )
                    )
                )
            )(else
                0
            )
    )


    (method (newRoom newRoomNumber)
        (super:newRoom(newRoomNumber))
    )


    (method (notify param1)
        (switch (param1)
            (case 1
                (send gNewAct:
                    view(777)
                    setCycle(Walk)
                    setScript(tActions)
                )
                = local0 0
            )
            (case 2
                (send gNewAct:dispose())
            )
            (case 3
                (send gNewAct:
                    view(777)
                    setMotion(0)
                    setScript(0)
                    ignoreActors()
                )
            )
            (case 4
                (if (== Random(1 4) 3)
                    = local6 ScriptID(511 0)
                    (send local6:init())
                )
            )
        )
    )

)
(instance public DUNELOOPER of Script
    (properties)

    (method (doit)
        (if ((== global104 1) or (== global104 3))
            (send gEgo:loop(
                (if ((< (send gEgo:heading) 45) or (> (send gEgo:heading) 315))
                    2
                )(else
                    (if ((>= (send gEgo:heading) 45) and (< (send gEgo:heading) 135))
                        0
                    )(else
                        (if ((>= (send gEgo:heading) 135) and (< (send gEgo:heading) 225))
                            3
                        )(else
                            1
                        )
                    )
                )
))
        )(else
            DirLoop(gEgo (send gEgo:heading))
        )
    )

)
(instance tActions of Script
    (properties)

    (method (doit)
        (asm
            lsg     global11
            lag     global13
            ne?     
            bnt     code_0a8a
            ret     
code_0a8a:  lsg     global104
            ldi     0
            ne?     
            bnt     code_0ab6
            lsl     local0
            ldi     1
            eq?     
            bnt     code_0ab6
            ldi     7
            aTop    state
            sal     local0
            pushi   #setMotion
            pushi   4
            class   MoveTo
            push    
            lsg     gGEgoX
            lsg     gGEgoY
            pushSelf
            pushi   217
            pushi   1
            pushi   2
            lag     gNewAct
            send    18
code_0ab6:  lsl     local0
            ldi     7
            eq?     
            bnt     code_0ae7
            pushi   #onControl
            pushi   0
            lag     gNewAct
            send    4
            push    
            ldi     2
            and     
            bnt     code_0ae7
            pushi   #view
            pushi   1
            pushi   777
            pushi   213
            pushi   1
            pushi   0
            lag     gNewAct
            send    12
            pushi   #changeState
            pushi   1
            pushi   8
            self    6
            ldi     8
            sal     local0
code_0ae7:  lsl     local0
            ldi     8
            eq?     
            bt      code_0af7
            lsl     local0
            ldi     7
            eq?     
            bnt     code_0b2b
code_0af7:  lsg     global104
            ldi     0
            eq?     
            bnt     code_0b2b
            pushi   #view
            pushi   1
            pushi   106
            pushi   216
            pushi   1
            pushi   2
            pushi   214
            pushi   1
            class   Avoid
            push    
            pushi   213
            pushi   4
            class   Chase
            push    
            lsg     gEgo
            pushi   10
            pushSelf
            lag     gNewAct
            send    30
            ldi     0
            aTop    seconds
            ldi     1
            aTop    state
            sal     local0
code_0b2b:  lsl     local0
            ldi     10
            ne?     
            bnt     code_0bbb
            pushi   5
            pushi   2
            pushi   #x
            pushi   0
            lag     gNewAct
            send    4
            push    
            ldi     9
            sub     
            push    
            pushi   #y
            pushi   0
            lag     gNewAct
            send    4
            push    
            ldi     3
            sub     
            push    
            pushi   #x
            pushi   0
            lag     gNewAct
            send    4
            push    
            ldi     9
            add     
            push    
            pushi   #y
            pushi   0
            lag     gNewAct
            send    4
            push    
            callk   OnControl, 10
            push    
            lag     global591
            and     
            bnt     code_0bbb
            lsl     local0
            ldi     1
            eq?     
            bnt     code_0ba8
            pushi   199
            pushi   #{-info-}
            lsl     local1
            lsl     local2
            ldi     3
            add     
            push    
            lag     gNewAct
            send    8
            ldi     0
            aTop    seconds
            ldi     1
            aTop    state
            pushi   #setAvoider
            pushi   1
            class   Avoid
            push    
            pushi   213
            pushi   4
            class   Chase
            push    
            lsg     gEgo
            pushi   10
            pushSelf
            lag     gNewAct
            send    18
            jmp     code_0bbb
code_0ba8:  pushi   #setMotion
            pushi   1
            pushi   0
            pushi   205
            pushi   0
            pushi   5
            pushi   1
            pushi   777
            lag     gNewAct
            send    16
code_0bbb:  pushi   3
            pushi   1
            pushi   #x
            pushi   0
            lag     gNewAct
            send    4
            push    
            pushi   #y
            pushi   0
            lag     gNewAct
            send    4
            push    
            callk   OnControl, 6
            push    
            ldi     0
            ne?     
            bnt     code_0bea
            pushi   #x
            pushi   0
            lag     gNewAct
            send    4
            sal     local1
            pushi   #y
            pushi   0
            lag     gNewAct
            send    4
            sal     local2
code_0bea:  lsl     local0
            ldi     1
            eq?     
            bt      code_0bfa
            lsl     local0
            ldi     7
            eq?     
            bnt     code_0ca5
code_0bfa:  pushi   #cel
            pushi   0
            lag     gNewAct
            send    4
            push    
            ldi     0
            eq?     
            bt      code_0c32
            pushi   #loop
            pushi   0
            lag     gNewAct
            send    4
            push    
            ldi     2
            lt?     
            bnt     code_0c24
            pushi   #cel
            pushi   0
            lag     gNewAct
            send    4
            push    
            ldi     4
            eq?     
            jmp     code_0c2f
code_0c24:  pushi   #cel
            pushi   0
            lag     gNewAct
            send    4
            push    
            ldi     3
            eq?     
code_0c2f:  bnt     code_0ca5
code_0c32:  pushi   3
            pushi   2
            pushi   #x
            pushi   0
            lag     gNewAct
            send    4
            push    
            pushi   #y
            pushi   0
            lag     gNewAct
            send    4
            push    
            callk   OnControl, 6
            sal     local3
            pushi   1
            push    
            call    localproc_0a65, 2
            sal     local3
            push    
            ldi     65535
            ne?     
            bnt     code_0ca5
            pushi   #view
            pushi   0
            lag     gNewAct
            send    4
            push    
            ldi     777
            ne?     
            bnt     code_0ca5
            pushi   #view
            pushi   1
            pushi   106
            pushi   6
            pushi   1
            pushi   #loop
            pushi   0
            lag     gNewAct
            send    4
            push    
            pushi   7
            pushi   1
            pushi   #cel
            pushi   0
            lag     gNewAct
            send    4
            push    
            pushi   205
            pushi   1
            pushi   1
            pushi   199
            pushi   2
            pushi   #x
            pushi   0
            lag     gNewAct
            send    4
            push    
            pushi   #y
            pushi   0
            lag     gNewAct
            send    4
            push    
            pushi   207
            pushi   0
            lofsa   footPrint
            send    36
code_0ca5:  lsg     global11
            lag     global13
            ne?     
            bnt     code_0cae
            ret     
code_0cae:  pushi   #doit
            pushi   0
            super   Script, 4
            ret     
        )
    )


    (method (changeState newState)
        (switch (= state newState)
            (case 0
                = seconds gSeconds
            )
            (case 1
                (if (== global104 0)
                    (send gNewAct:
                        view(106)
                        setMotion(Chase gEgo 10 self)
                        setAvoider(Avoid)
                        ignoreActors()
                    )
                    = local0 1
                )(else
                    = local0 (= state 7)
                    (send gNewAct:
                        view(106)
                        setMotion(MoveTo gGEgoX gGEgoY self)
                        ignoreControl(2)
                    )
                )
            )
            (case 2
                (if (<> global11 global13)
                    return 
                )
                = local0 10
                = global116 1
                proc0_2()
                (send gNewAct:
                    ignoreActors()
                    illegalBits(0)
                    posn((send gEgo:x) (send gEgo:y))
                )
                (send gEgo:
                    view(106)
                    setLoop(4)
                    cel(255)
                    ignoreHorizon()
                    illegalBits(0)
                    setCycle(End)
                    setPri((send gEgo:priority))
                    setStep(4 4)
                    setMotion(MoveTo + (send gEgo:x) 11 - (send gEgo:y) 15 self)
                )
                proc0_10()
            )
            (case 3
                (send gEgo:
                    setLoop(5)
                    setCycle(Fwd)
                )
                (send gNewAct:
                    view(106)
                    setLoop(6)
                    illegalBits(0)
                    setPri((send gEgo:priority))
                    setCycle(Fwd)
                    show()
                )
                = seconds 2
            )
            (case 4
                (send gNewAct:
                    view(106)
                    setLoop(7)
                    setPri((send gEgo:priority))
                    setCycle(Fwd)
                )
                = seconds 4
            )
            (case 5
                (send gEgo:hide())
                = newSound (Sound:new())
                (send newSound:
                    number(97)
                    loop(-1)
                    priority(99)
                    play()
                )
                (send gNewAct:
                    setLoop(8)
                    setCycle(Fwd)
                )
                = seconds 6
            )
            (case 6
                (send newSound:stop())
                (send gNewAct:
                    setLoop(8)
                    setCel(0)
                )
                proc0_17(901 0 14 16)
            )
            (case 8
                = seconds 12
                = local0 8
            )
            (case 9
                (if ((<> global104 1) and (<> global104 0))
                    = seconds (= state 8)
                )(else
                    (self:changeState(2))
                )
            )
        )
    )

)
