(version 2)
(include "sci.sh")
(exports
    0 rm640
)
(use "Main")
(use "n026")
(use "OccasionalCycle")
(use "JumpX")
(use "PolyPath")
(use "Polygon")
(use "CueObj")
(use "n958")
(use "Rev")
(use "Sound")
(use "Cycle")
(use "Game")
(use "User")
(use "View")
(use "Obj")
(script 640)


(local
    local0
    local1
    local2
    local3
    local4 =     1
    local5
    local6
    local7
    local8
    local9
    local10

)
(instance public rm640 of Rm
    (properties
        noun 6
        picture 640
        vanishingY -100
    )

    (method (init param1)
        proc958_0(128 31 2 43)
        proc0_2()
        (send global2:addObstacle((send ((Polygon:new())):
                type(3)
                init(145 135 85 128 70 106 64 126 99 145 72 151 72 158 119 155 144 171 246 170 200 139 184 134)
                yourself()
            )
))
        (skull:init())
        (skulls:init())
        (table:init())
        (bed:
            approachVerbs(4 2)
            init()
        )
        (hole:init())
        (skin:init())
        (send gOwner:
            x(78)
            y(126)
            init()
            setScale(135)
            normalize()
            changeGait(2 0)
        )
        (spear:init())
        (chest:init())
        (chief:
            init()
            approachVerbs(4 2)
            stopUpd()
        )
        (monkey:
            init()
            approachVerbs(29 4 2)
        )
        (spell:
            init()
            setScale(135)
            hide()
        )
        (masK:
            init()
            approachVerbs(4)
        )
        (send gQg3Walkers:addToFront(self))
        (super:init(rest param1))
        (send global2:setScript(enterHut))
        (send gGame:save(1))
    )


    (method (dispose param1)
        (send gQg3Walkers:delete(self))
        proc958_0(0 57 13 939)
        (super:dispose(rest param1))
    )


    (method (doVerb theVerb)
        (if (((== theVerb 3) and ((<> (send gOwner:view) 2) or (<= (send gOwner:trySkill(8 140)) 0))) and not local5)
            proc0_2()
            = local5 1
            = local0 1
            = local8 1
            (if (local9)
                (send gOwner:setMotion(PolyPath (send ((User:curEvent)):x) (send ((User:curEvent)):y)))
                (self:setScript(egoWalk))
            )(else
                = local9 1
                (send gOwner:setMotion(PolyPath (send ((User:curEvent)):x) (send ((User:curEvent)):y)))
                (self:setScript(chiefStir))
            )
        )(else
            (super:doVerb(theVerb))
        )
    )

)
(instance chiefStir of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                (chief:setCycle(Beg self))
                (send gOwner:setMotion(0))
            )
            (case 1
                (send gQg3Messager:say(1 2 8 0 self))
            )
            (case 2
                proc0_3()
                = local5 0
                (self:dispose())
            )
        )
    )

)
(instance castFetch of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                proc0_2()
                (send gOwner:setMotion(MoveTo 130 140 self))
            )
            (case 1
                proc0_12(gOwner chest self)
            )
            (case 2
                (send gOwner:
                    view(14)
                    setLoop(1)
                    cel(0)
                    setCycle(End self)
                )
            )
            (case 3
                (spell:
                    view(21)
                    x(- (send gOwner:x) 5)
                    y(- (send gOwner:y) 30)
                    moveSpeed(1)
                    cycleSpeed(1)
                    setStep(10 5)
                    setLoop(7)
                    setCycle(Walk)
                    setMotion(MoveTo 67 137 self)
                    show()
                )
            )
            (case 4
                (spell:setMotion(MoveTo (send gOwner:x) (- (send gOwner:y) 30) self))
            )
            (case 5
                (spell:hide())
                = cycles 3
            )
            (case 6
                (send gOwner:
                    normalize()
                    changeGait(2 0)
                )
                (if (local10)
                    (send gQg3Messager:say(3 6 18 0 self))
                )(else
                    = local10 1
                    (send gQg3Messager:say(3 4 0 0 self))
                )
            )
            (case 7
                (send gOwner:get(10 1))
                (send gOwner:get(0 8))
                = local10 1
                proc0_3()
                (self:dispose())
            )
        )
    )

)
(instance noEscape of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                proc0_2()
                (send gQg3Messager:say(1 2 1 0 self))
            )
            (case 1
                (send gOwner:setMotion(PolyPath 80 129 self))
            )
            (case 2
                proc0_3()
                = local4 0
                (self:dispose())
            )
        )
    )

)
(instance egoWalk of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                proc0_2()
                (chief:
                    startUpd()
                    setCycle(End self)
                )
            )
            (case 1
                = seconds 1
            )
            (case 2
                (chief:
                    x(249)
                    y(176)
                    cel(0)
                    setLoop(1)
                    cycleSpeed(10)
                    setCycle(CT 5 1 self)
                )
            )
            (case 3
                Palette(palSET_INTENSITY 0 255 1000)
                = cycles 2
            )
            (case 4
                Palette(palSET_INTENSITY 0 255 100)
                = cycles 1
            )
            (case 5
                (spell:dispose())
                (send gOwner:
                    view(43)
                    setLoop(2)
                    setCycle(End self)
                )
            )
            (case 6
                (switch (local8)
                    (case 1
                        proc26_0(8)
                    )
                    (case 2
                        proc26_0(9)
                    )
                    (case 3
                        proc26_0(10)
                    )
                    (case 4
                        proc26_0(2)
                    )
                    (case 5
                        proc26_0(6)
                    )
                    (case 6
                        proc26_0(7)
                    )
                    (case 7
                        proc26_0(14)
                    )
                )
            )
        )
    )

)
(instance enterHut of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                proc0_2()
                (send gOwner:setMotion(PolyPath 80 129 self))
            )
            (case 1
                (send gOwner:code(monkeyCheck))
                = local0 1
                = local4 0
                proc0_3()
                (self:dispose())
            )
        )
    )

)
(instance monkeyWay of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                (monkey:cue())
                (send gOwner:code(normCode))
                = cycles 1
            )
            (case 1
                (sFx:
                    number(929)
                    setLoop(-1)
                    play()
                )
                = seconds 10
            )
            (case 2
                (if (local2)
                    (self:dispose())
                )(else
                    proc0_2()
                    = local8 2
                    (send global2:setScript(egoWalk))
                )
            )
        )
    )

)
(instance monkeyOut of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                proc0_2()
                (monkeyWay:dispose())
                (sFx:
                    number(481)
                    play()
                )
                (send gQg3Messager:say(7 6 15 0 self))
            )
            (case 1
                (monkey:
                    setLoop(1)
                    setCycle(End)
                    cycleSpeed(4)
                    moveSpeed(2)
                    setMotion(JumpX + (monkey:x) 25 12 self)
                )
            )
            (case 2
                (monkey:
                    setCycle(0)
                    setMotion(JumpX -45 self)
                )
            )
            (case 3
                (monkey:
                    setPri(15)
                    cycleSpeed(2)
                    setLoop(3)
                    setCycle(Fwd)
                    setMotion(MoveTo - (send gOwner:x) 10 91 self)
                )
            )
            (case 4
                (monkey:
                    setPri(2)
                    setMotion(MoveTo 54 75 self)
                )
            )
            (case 5
                (send gOwner:addHonor(20))
                (monkey:dispose())
                proc0_3()
                (self:dispose())
            )
        )
    )

)
(instance castOpenMonkey of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                proc0_2()
                (monkeyWay:dispose())
                (send gOwner:setMotion(PolyPath 192 153 self))
            )
            (case 1
                proc0_12(gOwner monkey self)
            )
            (case 2
                (send gOwner:
                    view(15)
                    setLoop(3)
                    setCycle(End self)
                )
            )
            (case 3
                (monkey:
                    setLoop(1)
                    setCycle(End)
                    cycleSpeed(4)
                    moveSpeed(2)
                    setMotion(JumpX + (monkey:x) 25 12 self)
                )
            )
            (case 4
                (monkey:
                    setCycle(0)
                    setMotion(JumpX -45 self)
                )
            )
            (case 5
                (monkey:
                    setPri(15)
                    cycleSpeed(2)
                    setLoop(3)
                    setCycle(Fwd)
                    setMotion(MoveTo (monkey:x) 91 self)
                )
            )
            (case 6
                (monkey:
                    setPri(2)
                    setMotion(MoveTo 54 75 self)
                )
            )
            (case 7
                (send gOwner:
                    changeGait(2 0)
                    normalize()
                )
                (send gOwner:addHonor(20))
                (monkey:dispose())
                proc0_3()
                (self:dispose())
            )
        )
    )

)
(instance turnChief of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                (chief:setCycle(End self))
            )
            (case 1
                = seconds 2
            )
            (case 2
                (chief:setCycle(CT 0 -1 self))
            )
            (case 3
                (self:dispose())
            )
        )
    )

)
(instance castOpenOnChest of Script
    (properties)

    (method (changeState newState)
        (var temp0)
        (switch (= state newState)
            (case 0
                proc0_2()
                (send gOwner:setMotion(PolyPath 130 140 self))
            )
            (case 1
                proc0_12(gOwner chest self)
            )
            (case 2
                (send gOwner:
                    view(15)
                    setLoop(1)
                    setCycle(End self)
                )
            )
            (case 3
                (send gOwner:
                    changeGait(2 0)
                    normalize()
                )
                (if (not local6)
                    = local6 1
                    (if (not local3)
                        (chief:setScript(turnChief))
                        (send gQg3Messager:say(3 6 5))
                    )(else
                        = cycles 1
                    )
                )(else
                    (if (not local3)
                        = local8 5
                        (chest:cel(1))
                        (send global2:setScript(egoBack))
                    )(else
                        (chest:cel(1))
                        = local7 1
                    )
                )
                = cycles 5
            )
            (case 4
                proc0_3()
                (self:dispose())
            )
        )
    )

)
(instance egoBack of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                (send gOwner:
                    setCycle(Rev)
                    setLoop(1)
                    setMotion(PolyPath + (send gOwner:x) 15 (send gOwner:y) self)
                )
            )
            (case 1
                (send gOwner:normalize())
                (send global2:setScript(egoWalk))
            )
        )
    )

)
(instance gripSpear of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                proc0_2()
                (send gOwner:setMotion(PolyPath 181 135 self))
            )
            (case 1
                (send gOwner:
                    view(31)
                    loop(0)
                    setCycle(CT 2 1 self)
                )
            )
            (case 2
                (send gOwner:setCycle(Beg self))
                (spear:dispose())
            )
            (case 3
                (send gOwner:
                    view(2)
                    setCycle(Walk)
                    changeGait(2 0)
                    get(45)
                    addHonor(-50)
                    solvePuzzle(304 10 4)
                )
                (send gQg3Messager:say(1 6 13 0 self))
            )
            (case 4
                proc0_3()
                (self:dispose())
            )
        )
    )

)
(instance egoTouch of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                proc0_2()
                (send gOwner:
                    view(31)
                    loop(0)
                    setCycle(End self)
                )
                (send gLongSong3:
                    number(641)
                    setLoop(1)
                    play()
                )
                (send global2:setScript(egoWalk))
            )
            (case 1
                (send gOwner:
                    changeGait(2 0)
                    normalize()
                )
                = cycles 1
            )
            (case 2
                (send gOwner:
                    setCycle(Rev)
                    setLoop(0)
                    setMotion(MoveTo - (send gOwner:x) 25 + (send gOwner:y) 5 self)
                )
            )
            (case 3
                (send gOwner:normalize())
                = local8 3
                (self:dispose())
            )
        )
    )

)
(instance openChest of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                (send gOwner:setMotion(PolyPath 78 152 self))
            )
            (case 1
                (send gQg3Messager:say(3 6 2 0 self))
            )
            (case 2
                (self:dispose())
            )
        )
    )

)
(instance getChest of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                (send gOwner:setMotion(PolyPath 78 152 self))
            )
            (case 1
                (send gOwner:
                    view(4)
                    loop(1)
                    cel(0)
                    setCycle(CT 
                        (if (local10)
                            1
                        )(else
                            3
                        )
 1 self)
                )
                (if (not local7)
                    (chest:cel(1))
                )
            )
            (case 2
                (if (not local3)
                    = local8 5
                    (send global2:setScript(egoBack))
                )(else
                    (self:cue())
                )
            )
            (case 3
                (send gOwner:setCycle(CT 0 -1 self))
            )
            (case 4
                (send gOwner:
                    normalize()
                    changeGait(2 0)
                )
                (if (local10)
                    (send gQg3Messager:say(3 6 18 0 self))
                )(else
                    (send gQg3Messager:say(3 4 0 0 self))
                )
            )
            (case 5
                (send gOwner:get(10 1))
                (send gOwner:get(0 8))
                = local10 1
                (self:dispose())
            )
        )
    )

)
(instance pickLock of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                proc0_2()
                (send gOwner:setMotion(PolyPath 78 152 self))
            )
            (case 1
                proc0_12(gOwner chest)
                = cycles (+ (send gOwner:cycleSpeed) 5)
            )
            (case 2
                (if (not local6)
                    (send gOwner:useSkill(9 200))
                    = local6 1
                    (if (not local3)
                        (chief:setScript(turnChief))
                        (send gQg3Messager:say(3 6 3))
                    )(else
                        (send gQg3Messager:say(3 6 20))
                    )
                )(else
                    (if (not local3)
                        = local8 5
                        (send global2:setScript(egoBack))
                    )(else
                        (chest:cel(1))
                        = local7 1
                    )
                )
                = cycles 5
            )
            (case 3
                proc0_3()
                (self:dispose())
            )
        )
    )

)
(instance oilChest of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                (send gOwner:setMotion(PolyPath 78 152 self))
            )
            (case 1
                (send gOwner:
                    view(4)
                    loop(1)
                    cel(0)
                    setCycle(CT 3 1 self)
                )
            )
            (case 2
                (send gOwner:setCycle(CT 0 -1 self))
            )
            (case 3
                (send gOwner:
                    changeGait(2 0)
                    normalize()
                )
                (send gQg3Messager:say(3 6 6 0 self))
            )
            (case 4
                (self:dispose())
            )
        )
    )

)
(instance monkeyCheck of Code
    (properties)

    (method (doit)
        (if ((& (send gOwner:onControl()) $0010) and not local4)
            = local4 1
            (if ((send gOwner:has(45)))
                (send global2:newRoom(630))
            )(else
                (send global2:setScript(noEscape))
            )
        )(else
            (if (((> (send gOwner:x) 110) and (& local0 $0001)) and not local2)
                = local0 2
                (monkey:setScript(monkeyWay))
            )
        )
    )

)
(instance normCode of Code
    (properties)

    (method (doit)
        (if ((& (send gOwner:onControl()) $0010) and not local4)
            = local4 1
            (if ((send gOwner:has(45)))
                (send global2:newRoom(630))
            )(else
                (send global2:setScript(noEscape))
            )
        )
    )

)
(instance chief of Actor
    (properties
        x 258
        y 173
        z 30
        noun 14
        approachX 212
        approachY 146
        view 641
        signal $1000
    )

    (method (doVerb theVerb)
        (switch (theVerb)
            (case 1
                (send gQg3Messager:say(noun 1 0))
            )
            (case 4
                = local8 4
                (send gOwner:
                    setLoop(1)
                    setMotion(PolyPath - (send gOwner:x) 65 (send gOwner:y))
                )
                (send global2:setScript(egoWalk))
            )
            (case 2
                = local8 6
                (send gOwner:
                    setLoop(1)
                    setMotion(PolyPath - (send gOwner:x) 65 (send gOwner:y))
                )
                (send global2:setScript(egoWalk))
            )
            (case 20
                = local8 7
                (send global2:setScript(egoWalk))
            )
            (case 82
                = local8 4
                (send global2:setScript(egoWalk))
            )
            (default 
                (super:doVerb(theVerb))
            )
        )
    )

)
(instance monkey of Actor
    (properties
        x 137
        y 91
        noun 15
        approachX 137
        approachY 133
        view 645
    )

    (method (doVerb theVerb)
        (switch (theVerb)
            (case 29
                = local2 1
                (if (not local1)
                    = local1 1
                    (send gOwner:solvePuzzle(302 5 4))
                    (send gQg3Messager:say(7 6 16))
                    (if (& local0 $0002)
                        (monkey:setCycle(0))
                    )
                )
            )
            (case 4
                = local2 1
                (if (not proc0_6(90))
                    proc0_4(90)
                    (send gOwner:solvePuzzle(303 5 4))
                    (send global2:setScript(monkeyOut))
                )(else
                    (super:doVerb(theVerb))
                )
            )
            (case 75
                = local2 1
                (if (not proc0_6(90))
                    proc0_4(90)
                    (send global2:setScript(castOpenMonkey))
                )(else
                    (super:doVerb(theVerb))
                )
            )
            (default 
                (super:doVerb(theVerb))
            )
        )
    )


    (method (cue)
        (self:setCycle(OccasionalCycle self 1 10 50))
    )

)
(instance spell of Actor
    (properties
        x 240
        y 128
        view 21
        signal $4000
    )
)
(instance spear of Prop
    (properties
        x 193
        y 133
        noun 5
        approachX 196
        approachY 128
        view 640
        loop 1
        signal $4000
    )

    (method (doVerb theVerb)
        (switch (theVerb)
            (case 4
                (send global2:setScript(gripSpear))
            )
            (default 
                (super:doVerb(theVerb))
            )
        )
    )

)
(instance chest of View
    (properties
        x 75
        y 138
        noun 3
        view 640
        priority 10
        signal $4010
    )

    (method (doVerb theVerb)
        (switch (theVerb)
            (case 4
                (if (not local6)
                    (send global2:setScript(openChest))
                )(else
                    (send global2:setScript(getChest))
                )
            )
            (case 75
                (if (local7)
                    (send gQg3Messager:say(1 6 11))
                )(else
                    (send global2:setScript(castOpenOnChest))
                )
            )
            (case 17
                (if (local6)
                    (send gQg3Messager:say(1 6 19))
                )(else
                    (send global2:setScript(pickLock))
                )
            )
            (case 35
                (if (local7)
                    (send gQg3Messager:say(1 6 12))
                )(else
                    = local3 1
                    (send global2:setScript(oilChest))
                )
            )
            (case 82
                (if (local7)
                    (send global2:setScript(castFetch))
                )(else
                    (send gQg3Messager:say(1 6 21))
                )
            )
            (default 
                (super:doVerb(theVerb))
            )
        )
    )

)
(instance masK of Feature
    (properties
        x 229
        y 95
        noun 2
        onMeCheck $0008
        approachX 206
        approachY 125
    )

    (method (doVerb theVerb)
        (switch (theVerb)
            (case 4
                proc0_2()
                (send gOwner:setScript(egoTouch))
            )
            (default 
                (super:doVerb(theVerb))
            )
        )
    )

)
(instance skull of Feature
    (properties
        x 49
        y 80
        noun 8
        nsTop 66
        nsLeft 37
        nsBottom 95
        nsRight 61
    )
)
(instance skulls of Feature
    (properties
        x 90
        y 45
        noun 9
        nsTop 39
        nsLeft 80
        nsBottom 52
        nsRight 101
    )
)
(instance table of Feature
    (properties
        x 96
        y 161
        noun 10
        nsTop 153
        nsLeft 45
        nsBottom 169
        nsRight 148
    )
)
(instance bed of Feature
    (properties
        x 246
        y 145
        noun 11
        nsTop 134
        nsLeft 212
        nsBottom 156
        nsRight 281
        approachX 212
        approachY 146
    )

    (method (doVerb theVerb)
        (if ((== theVerb 4) or (== theVerb 2))
            (chief:doVerb(theVerb))
        )(else
            (super:doVerb(theVerb))
        )
    )

)
(instance hole of Feature
    (properties
        x 182
        y 107
        noun 12
        nsTop 97
        nsLeft 165
        nsBottom 118
        nsRight 199
    )
)
(instance skin of Feature
    (properties
        x 218
        y 67
        noun 13
        nsTop 47
        nsLeft 182
        nsBottom 88
        nsRight 254
    )
)
(instance sFx of Sound
    (properties)
)
