(include "sci.sh")
(use "Main")
(use "Class_255_0")
(use "RegionPath")
(use "PolyPath")
(use "Polygon")
(use "Class_950_0")
(use "MoveFwd")
(use "n958")
(use "StopWalk")
(use "SysWindow")
(use "Sound")
(use "Cycle")
(use "Game")
(use "PicView")
(use "Obj")
(script 704)


(local
    local0
    local1
    gEgoX
    local3
    local4
    local5
    local6
    local7
    local8
    theRegister
    local10
    local11
    local12[100]
    local112[40]
    local152[271] = (32767 515 23 170 166 170 207 124 340 124 32767 520 65516 124 53 124 104 143 203 143 255 124 340 124 32767 525 65516 124 69 124 65516 65518 32767 510 158 240 106 161 65516 161 32767 505 340 163 65 163 255 163 271 240 32767 520 230 130 230 172 92 172 49 152 65516 152 32767 515 340 154 135 154 117 166 79 166 61 73 96 73 177 65528 32767 500 63 240 173 120 340 120 32767 505 65516 124 44 124 78 31 44 124 65516 124 32767 500 340 120 173 120 63 240 32767 515 177 65518 136 34 340 34 32767 520 65516 34 91 34 111 22 204 22 233 34 340 34 32767 525 65516 34 140 34 95 65518 32767 510 267 240 147 120 65516 120 32767 505 340 124 271 124 236 31 271 124 340 124 32767 510 65516 120 147 120 267 240 32767 525 105 65518 140 34 227 34 340 111 340 180 203 180 124 91 65516 91 32767 520 340 91 241 91 209 103 103 103 81 91 65516 91 32767 515 340 91 77 91 96 73 61 73 79 166 117 166 135 154 340 154 32767 520 65516 152 78 152 78 124 32767 505 42 240 65 163 340 163 32767 510 65516 161 106 161 158 240 32767 525 65516 65518 69 124 65516 124 32767 520 340 124 255 124 203 143 104 143 53 124 65516 124 32767 515 340 124 207 124 166 170 23 170 32768 )

)
(procedure public (proc704_6 param1)
    (if (== gNumber gTheGNumber)
        (voiceWindow:
            color(global143)
            back(gColor)
        )
        Print(rest param1 #window voiceWindow #title "Metallic Voice:" #mode 1 #at 160 10 #width 150 #time 3 #dispose)
    )
)


(procedure (localproc_002a param1)
    (var temp0, temp1)
    = temp0 (send ((send param1:mover)):value)
    (if (< temp0 50)
        = temp1 2
    )(else
        (if (< temp0 72)
            = temp1 1
        )(else
            (if (< temp0 208)
                = temp1 3
            )(else
                (if (< temp0 224)
                    = temp1 1
                )(else
                    = temp1 2
                )
            )
        )
    )
    return temp1
)


(procedure (localproc_007e)
    (var brainFormatting)
    (switch (= brainFormatting (brain:formatting))
        (case 5000
            StrCpy(@local112 "EXCELLENT")
            = local11 global137
        )
        (case 3000
            StrCpy(@local112 "VERY GOOD")
        )
        (case 2000
            StrCpy(@local112 "GOOD")
        )
        (case 1500
            StrCpy(@local112 "FAIR")
        )
        (case 1000
            StrCpy(@local112 "DANGER")
            = local11 global135
        )
        (case 500
            StrCpy(@local112 "CRITICAL")
            = local11 global143
        )
    )
    (if (((< 0 brainFormatting) and (< brainFormatting 3000)) and not (% brainFormatting 250))
        proc704_6(Format(@local12 704 0 brainFormatting @local112))
        (alertSound:play())
    )
    (if ((brainFormatting and (<> gNumber 544)) and (<> (send global2:curPic) 699))
        (if (not local10)
            proc0_12("Time to Format:" 67 0 180 70 100 28 local11)
        )
        proc0_12(local10)
        = local10 proc0_12(Format(@local12 704 1 brainFormatting) 67 90 180 70 25 28 local11)
    )
)


(procedure (localproc_023b param1)
    return 
        (if (<> (send param1:level) (brain:level))
            0
        )(else
            (if (Btest(38))
                0
            )(else
                (if (== (send param1:lastRoom) gNumber)
                    0
                )(else
                    (if (== (send param1:room) (send global2:north))
                        1
                    )(else
                        (if (== (send param1:room) (send global2:south))
                            3
                        )(else
                            (if (== (send param1:room) (send global2:east))
                                2
                            )(else
                                (if (== (send param1:room) (send global2:west))
                                    4
                                )(else
                                    0
                                )
                            )
                        )
                    )
                )
            )
        )
)


(procedure (localproc_02e3 param1)
    return 
        (if ((< (send gEgo:heading) 45) or (> (send gEgo:heading) 305))
            (switch (param1)
                (case 1
                    return 0
                )
                (case 3
                    return 1
                )
                (case 2
                    return 2
                )
                (case 4
                    return 3
                )
            )
        )(else
            (if (< (send gEgo:heading) 135)
                (switch (param1)
                    (case 1
                        return 3
                    )
                    (case 3
                        return 2
                    )
                    (case 2
                        return 0
                    )
                    (case 4
                        return 1
                    )
                )
            )(else
                (if (< (send gEgo:heading) 235)
                    (switch (param1)
                        (case 1
                            return 1
                        )
                        (case 3
                            return 0
                        )
                        (case 2
                            return 3
                        )
                        (case 4
                            return 2
                        )
                    )
                )(else
                    (switch (param1)
                        (case 1
                            return 2
                        )
                        (case 3
                            return 3
                        )
                        (case 2
                            return 1
                        )
                        (case 4
                            return 0
                        )
                    )
                )
            )
        )
)


(procedure (localproc_03f8 param1)
    (var temp0[40])
    Print(Format(@temp0 704 2 
            (switch (localproc_02e3(param1))
                (case 0
                    "ahead of you"
                )
                (case 1
                    "behind you"
                )
                (case 3
                    "your left"
                )
                (case 2
                    "your right"
                )
            )
) (send gLongSong2:
            number(845)
            vol(0)
            loop(-1)
            play()
            fade(127 1 10 0)
        )
)
)


(instance voiceWindow of SysWindow
    (properties)
)
(class brain of Rgn
    (properties
        script 0
        number 0
        timer 0
        keep 0
        initialized 0
        lookStr 0
        level 1
        formatting 0
        lastRoom 0
        exitDir 0
    )

    (method (init param1)
        (var temp0, temp1, temp2)
        proc958_0(128 501 28 506)
        (super:init(rest param1))
        (if (not Btest(38) and (<> gNumber 541))
            (droid1:init())
            (droid2:init())
        )
        (switch (level)
            (case 1
                = temp0 2
            )
            (case 2
                = temp0 3
            )
            (case 3
                = temp0 -1
            )
        )
        (if (== gNumber 541)
            (send gEgo:
                view(0)
                setCycle(StopWalk 4)
                headView(4)
                illegalBits(0)
                setPri(temp0)
                normal(1)
            )
        )(else
            (send gEgo:
                view(3)
                setCycle(StopWalk 510)
                headView(510)
                illegalBits(0)
                setPri(temp0)
                normal(1)
            )
        )
        = local10 0
        (if (proc999_5(gNumber 541 544))
            = temp2 0
        )(else
            (if ((brain:formatting))
                = temp2 869
            )(else
                (if (== gNumber 520)
                    = temp2 849
                )(else
                    (if (== gNumber 514)
                        = temp2 850
                    )(else
                        = temp2 20
                    )
                )
            )
        )
        (if (not temp2)
            (send gLongSong:fade())
        )(else
            (if (<> (send gLongSong:number) temp2)
                (send gLongSong:
                    number(temp2)
                    vol(127)
                    loop(-1)
                    play()
                )
            )
        )
        = temp1 localproc_023b(droid1)
        (if (= temp1 localproc_023b(droid1))
            localproc_03f8(= temp1 localproc_023b(droid1))
        )
        = temp1 localproc_023b(droid2)
        (if (= temp1 localproc_023b(droid2))
            localproc_03f8(= temp1 localproc_023b(droid2))
        )
    )


    (method (doit)
        (super:doit())
        (if (formatting)
            (if (local10 and (== (send global2:curPic) 699))
                = local10 0
            )
            localproc_007e()
            (if (not --formatting)
                proc0_10(0 5)
            )
        )
        (if ((send global2:script))
        )(else
            (if (proc0_5(gEgo 8) and (<> gNumber 150))
                (send global2:setScript(rgnExitScript 0 exitDir))
            )
        )
    )


    (method (newRoom param1)
        (var temp0)
        (if ((droid1:mover))
            (droid1:setMotion(0))
        )
        (if ((droid2:mover))
            (droid2:setMotion(0))
        )
        (if (not = keep proc999_5(param1 150 500 505 510 514 515 520 525 541 544 545))
            (send gLongSong:fade())
        )
        = initialized 0
        (if (gTheNewDialog)
            (send gTheNewDialog:dispose())
        )
        (send gLongSong2:fade())
    )


    (method (makePolygon param1)
        (var newPolygon)
        = newPolygon (Polygon:new())
        (send newPolygon:
            init(rest param1)
            type(2)
        )
        (send global2:addObstacle(newPolygon))
    )

)
(class SecurityDroid of Actor
    (properties
        x 0
        y 0
        z 0
        heading 0
        noun 0
        nsTop 0
        nsLeft 0
        nsBottom 0
        nsRight 0
        description "security droid"
        sightAngle 26505
        actions 26505
        onMeCheck 26505
        lookStr "It's a security droid!"
        yStep 2
        view 0
        loop 0
        cel 0
        priority 0
        underBits 0
        signal 0
        lsTop 0
        lsLeft 0
        lsBottom 0
        lsRight 0
        brTop 0
        brLeft 0
        brBottom 0
        brRight 0
        palette 0
        cycleSpeed 0
        script 0
        cycler 0
        timer 0
        illegalBits 32768
        xLast 0
        yLast 0
        xStep 3
        moveSpeed 0
        blocks 0
        baseSetter 0
        mover 0
        looper 0
        viewer 0
        avoider 0
        code 0
        room 0
        lastRoom 0
        level 0
        path 0
        attacks 0
        body 0
    )

    (method (init param1)
        = illegalBits 0
        = signal 26624
        (self:setCycle(Fwd))
        (if (IsObject(path))
            (self:setMotion(path))
        )
        (super:init(rest param1))
        (if (body)
            (send body:init())
        )
        = attacks 0
        (self:doit())
        (if (body)
            (send body:doit())
        )
    )


    (method (doit)
        (var temp0)
        (super:doit())
        (if (script)
        )(else
            (if (not mover)
                (if (<> gNumber 545)
                    (self:setMotion(path))
                )
            )(else
                (switch (level)
                    (case 1
                        (self:setPri(2))
                    )
                    (case 2
                        (self:setPri(3))
                    )
                    (default 
                        (self:setPri(-1))
                    )
                )
                (if (proc999_5(room gNumber (brain:lastRoom)) and (== level (brain:level)))
                    = temp0 (self:distanceTo(gEgo))
                    (if (== attacks 3)
                        (self:attack(1))
                    )(else
                        (if (< temp0 50)
                            (self:attack(1))
                        )(else
                            (if (< temp0 100)
                                (self:attack(0))
                            )
                        )
                    )
                )
            )
        )
    )


    (method (onMe param1)
        (if ((super:onMe(rest param1)))
        )(else
            (send body:onMe(rest param1))
        )
    )


    (method (attack param1)
        (if ((<> room (send global2:curPic)) and (<> gNumber 545))
        )(else
            (if (not proc999_4(0 0 320 190 (send gEgo:x) (send gEgo:y)))
            )(else
                (if (== gNumber 545)
                    (if (not mover)
                    )(else
                        (if ((send mover:isKindOf(RegionPath)))
                            (self:setMotion(PolyPath (send mover:x) (send mover:y)))
                        )
                    )
                )(else
                    ++attacks
                    (self:setScript(shootEgo 0 param1))
                )
            )
        )
    )

)
(instance public droid1 of SecurityDroid
    (properties
        z 44
        view 506
        cycleSpeed 1
    )

    (method (init param1)
        = path droidPath1
        = body droid1Body
        (super:init(rest param1))
    )


    (method (doit)
        (super:doit())
        (switch (room)
            (case 525
                (if ((self:inRect(0 0 64 40)) and (== level 3))
                    (self:setPri(4))
                )
            )
            (case 515
                (if ((< x 120) and (== level 2))
                    (self:setPri(5))
                )
            )
        )
    )

)
(instance public droid2 of SecurityDroid
    (properties
        z 44
        view 506
        cycleSpeed 1
    )

    (method (init param1)
        = path droidPath2
        = body droid2Body
        (super:init(rest param1))
    )


    (method (doit)
        (super:doit())
        (switch (room)
            (case 525
                (if ((self:inRect(0 0 64 40)) and (== level 3))
                    (self:setPri(4))
                )
            )
            (case 515
                (if ((< x 120) and (== level 2))
                    (self:setPri(5))
                )
            )
        )
    )

)
(instance droid1Body of Prop
    (properties
        view 506
        loop 1
    )

    (method (doit)
        (super:doit())
        (if ((droid1:loop))
        )(else
            (self:
                x((droid1:x))
                y((droid1:y))
                z(- (droid1:z) 44)
                setPri((droid1:priority))
                signal(| (droid1:signal) $4000)
            )
        )
    )

)
(instance droid2Body of Prop
    (properties
        view 506
        loop 1
    )

    (method (doit)
        (super:doit())
        (if ((droid2:loop))
        )(else
            (self:
                x((droid2:x))
                y((droid2:y))
                z(- (droid2:z) 44)
                setPri((droid2:priority))
                signal(| (droid2:signal) $4000)
            )
        )
    )

)
(instance droidPath1 of RegionPath
    (properties
        theRegion 704
    )

    (method (next)
        (if (proc999_5(value 71 205) and (<> gNumber 515))
            = value (+ value 2)
            (send client:posn(local152[++value] local152[++value]))
        )
        (super:next())
    )


    (method (nextRoom param1)
        (var temp0)
        (send client:lastRoom((send client:room)))
        (super:nextRoom(rest param1))
        (if (== (send client:lastRoom) gNumber)
            (send gLongSong2:fade())
        )
        = temp0 localproc_023b(client)
        (if (= temp0 localproc_023b(client))
            localproc_03f8(= temp0 localproc_023b(client))
        )
    )


    (method (at param1)
        (send client:
            room(currentRoom)
            level(localproc_002a(client))
        )
        return local152[param1]
    )

)
(instance droidPath2 of RegionPath
    (properties
        value 77
        theRegion 704
    )

    (method (next)
        (if (proc999_5(value 71 205) and (<> gNumber 515))
            = value (+ value 2)
            (send client:posn(local152[++value] local152[++value]))
        )
        (super:next())
    )


    (method (nextRoom param1)
        (var temp0)
        (send client:lastRoom((send client:room)))
        (super:nextRoom(rest param1))
        (if (== (send client:lastRoom) gNumber)
            (send gLongSong2:fade())
        )
        = temp0 localproc_023b(client)
        (if (= temp0 localproc_023b(client))
            localproc_03f8(= temp0 localproc_023b(client))
        )
    )


    (method (at param1)
        (send client:
            room(currentRoom)
            level(localproc_002a(client))
        )
        return local152[param1]
    )

)
(instance blast of Prop
    (properties)
)
(instance public timerSound of Sound
    (properties
        number 879
    )
)
(instance laserSound of Sound
    (properties
        number 105
    )
)
(instance alertSound of Sound
    (properties
        number 101
    )
)
(instance shootEgo of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                (laserSound:play())
                (send client:setMotion(0))
                (if (register)
                    = gEgoX (send gEgo:x)
                    = local3 (- (send gEgo:y) 15)
                )(else
                    = gEgoX (+ (send gEgo:x) (- Random(1 20) 10))
                    = local3 (+ (send gEgo:y) (- Random(1 20) 10))
                )
                = local0 proc999_2(320 proc999_3(0 (send client:x)))
                = local1 proc999_2(190 proc999_3(0 (- (send client:y) 44)))
                = local4 (- proc999_2(local1 local3) 1)
                = local6 (+ proc999_3(local1 local3) 1)
                = local5 (- proc999_2(local0 gEgoX) 1)
                = local7 (+ proc999_3(local0 gEgoX) 1)
                = local8 Graph(7 local4 local5 local6 local7 1)
                Graph(4 local1 local0 local3 gEgoX global143 -1 -1)
                Graph(13 local4 local5 local6 local7 1)
                = cycles 1
            )
            (case 1
                Graph(8 local8)
                Graph(13 local4 local5 local6 local7 1)
                (send client:setMotion(PolyPath (send gEgo:x) (send gEgo:y)))
                (if (register)
                    (send gEgo:
                        view(511)
                        setLoop(
                            (if (< (send client:x) (send gEgo:x))
                                0
                            )(else
                                1
                            )
)
                        setCel(0)
                        setMotion(0)
                        setCycle(End self)
                    )
                )(else
                    (blast:
                        view(28)
                        x(gEgoX)
                        y(local3)
                        signal(24576)
                        init()
                        setCycle(End self)
                    )
                )
            )
            (case 2
                (if (register)
                    proc0_10(8 20)
                )(else
                    (blast:dispose())
                )
                (self:dispose())
            )
        )
    )

)
(instance public terminal1 of Feature
    (properties
        description "terminal interface"
        sightAngle 90
    )

    (method (doVerb param1 param2 param3)
        (if (<> (brain:level) 1)
            Print("You can't reach it from this level.")
        )(else
            (switch (param1)
                (case 1
                    (send global2:setScript(pocketPalScript self 0))
                )
                (case 3
                    (if (== param2 15)
                        (send global2:setScript(pocketPalScript 0 self))
                    )
                )
                (default 
                    (super:doVerb(param1 rest param3))
                )
            )
        )
    )

)
(instance public terminal2 of Feature
    (properties
        description "terminal interface"
        sightAngle 90
    )

    (method (doVerb param1 param2 param3)
        (if (<> (brain:level) 2)
            Print("You can't reach it from this level.")
        )(else
            (switch (param1)
                (case 1
                    (send global2:setScript(pocketPalScript self 0))
                )
                (case 3
                    (if (== param2 15)
                        (send global2:setScript(pocketPalScript 0 self))
                    )
                )
                (default 
                    (super:doVerb(param1 rest param3))
                )
            )
        )
    )

)
(instance public terminal3 of Feature
    (properties
        description "terminal interface"
        sightAngle 90
    )

    (method (doVerb param1 param2 param3)
        (if (<> (brain:level) 3)
            Print("You can't reach it from this level.")
        )(else
            (switch (param1)
                (case 1
                    (send global2:setScript(pocketPalScript self 0))
                )
                (case 3
                    (if (== param2 15)
                        (send global2:setScript(pocketPalScript 0 self))
                    )
                )
                (default 
                    (super:doVerb(param1 rest param3))
                )
            )
        )
    )

)
(instance pocketPalPanel of View
    (properties
        x 35
        y 46
        view 501
        priority 14
        signal 16
    )
)
(instance pocketPalPlug of Prop
    (properties
        x 36
        y 41
        description "terminal interface"
        lookStr "\"Slot A (Insert terminal plug here)\""
        view 501
        loop 1
        priority 15
        signal 16
    )

    (method (doit)
        (super:doit())
        (if (> (send gEgo:distanceTo(theRegister)) 30)
            (self:dispose())
        )
    )


    (method (dispose)
        (pocketPalPanel:dispose())
        (super:dispose())
    )


    (method (cue)
        (self:dispose())
    )

)
(instance pocketPalScript of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                (if (register)
                    = theRegister register
                )(else
                    = theRegister caller
                    = caller 0
                )
                proc0_2()
                (if (> (send gEgo:distanceTo(theRegister)) 30)
                    (send gEgo:setMotion(PolyPath (send theRegister:x) (send theRegister:y) self))
                )(else
                    = cycles 1
                )
            )
            (case 1
                (if (register)
                    (if (not == (send ((send gInv:at(11))):owner) 1)
                        Print("The PocketPal(tm) doesn't plug in directly.  Maybe you need some kind of adapter.")
                    )(else
                        (if (not == (send ((send gInv:at(4))):owner) 1)
                            Print("You turn the power on, but nothing happens.")
                        )(else
                            (if (<> (send ((send gInv:at(11))):state) global170)
                                Print("The adapter plug doesn't seem to fit into Slot A of the interface.")
                            )(else
                                (brain:lastRoom(gNumber))
                                (send global2:newRoom(545))
                            )
                        )
                    )
                )(else
                    (pocketPalPanel:init())
                    (pocketPalPlug:
                        cel(global170)
                        init()
                    )
                )
                proc0_3()
                (self:dispose())
            )
        )
    )

)
(instance rgnExitScript of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                proc0_2()
                (send gEgo:setHeading(register self))
            )
            (case 1
                (send gEgo:setMotion(MoveFwd 50 self))
            )
            (case 2
                proc0_3()
                (self:dispose())
            )
        )
    )

)
