(include "sci.sh")
(use "Main")
(use "Class_255_0")
(use "Sound")
(use "Cycle")
(use "Game")
(use "InvI")
(use "PV")
(use "Obj")
(script 16)


(local
    newAct
    newAct_2
    newAct_3
    newProp
    local4
    local5
    local6
    local7
    local8

)
(instance zTheme of Sound
    (properties)
)
(instance public Room16 of Rm
    (properties
        picture 16
    )

    (method (init)
        = north 10
        = south 22
        = east 17
        = west 15
        = horizon 80
        = global101 0
        = global186 0
        (if (global100)
            = picture 116
        )
        Load(rsVIEW 47)
        Load(rsVIEW 21)
        (if (<= (send gEgo:y()) (+ horizon 1))
            (send gEgo:y((+ horizon 1)))
        )
        (if (== gPicture 22)
            (send gEgo:x(158))
        )
        (super:init())
        (self:setRegions(510))
        (if (global100)
            Load(rsVIEW 261)
            Load(rsVIEW 263)
            Load(rsVIEW 265)
            Load(rsVIEW 36)
            Load(rsVIEW 46)
            Load(rsVIEW 45)
            = newAct (Act:new())
            = newAct_2 (Act:new())
            = newAct_3 (Act:new())
            (send newAct:
                view(260)
                loop(0)
                cel(0)
                xStep(2)
                yStep(1)
                posn(250 117)
                setScript(z1Actions)
                init()
                hide()
            )
            (send newAct_2:
                view(262)
                loop(0)
                cel(0)
                xStep(2)
                yStep(1)
                posn(70 124)
                setScript(z2Actions)
                init()
                hide()
            )
            (send newAct_3:
                view(264)
                loop(0)
                cel(0)
                xStep(2)
                yStep(1)
                posn(103 173)
                setScript(z3Actions)
                init()
                hide()
            )
        )
        (send gEgo:
            view(2)
            observeControl(16384)
            init()
        )
        = global105 0
        (send global2:setScript(holeActions))
        = local6 1
        (while (<= local6 global113)
            (if (== gNewPropX[(= local5 (* (- local6 1) 3))] gGNewPropX)
                (send ((View:new())):
                    view(528)
                    loop(0)
                    cel(6)
                    posn(gNewPropX[+ local5 1] gNewPropX[+ local5 2])
                    setPri(0)
                    addToPic()
                    ignoreActors()
                )
            )
            ++local6
        )
    )


    (method (dispose)
        (send gSounds:eachElementDo(172))
        (zTheme:dispose())
        (super:dispose())
    )


    (method (handleEvent pEvent)
        (if ((send pEvent:claimed()))
            return 1
        )
        return 
            (if (== (send pEvent:type()) 128)
                (if (Said('look/gravestone'))
                    Print(16 0)
                )(else
                    (if (Said('read,look/epitaph,gravestone,boulder'))
                        (if (& (send gEgo:onControl(0)) $0008)
                            Print(16 1 60 1 134 -1 15 140 290)
                        )(else
                            (if (& (send gEgo:onControl(0)) $0020)
                                Print(16 2 60 1 134 -1 15 140 290)
                            )(else
                                (if (& (send gEgo:onControl(0)) $0100)
                                    Print(16 3 60 1 134 -1 15 140 290)
                                )(else
                                    (if (& (send gEgo:onControl(0)) $0200)
                                        Print(16 4 60 1 134 -1 15 140 290)
                                    )(else
                                        (if (& (send gEgo:onControl(0)) $0004)
                                            Print(16 5 60 1 134 -1 15 140 290)
                                        )(else
                                            (if (& (send gEgo:onControl(0)) $0002)
                                                Print(16 6 60 1 134 -1 15 140 290)
                                            )(else
                                                (if (& (send gEgo:onControl(0)) $0080)
                                                    Print(16 7 60 1 134 -1 15 140 290)
                                                )(else
                                                    (if (& (send gEgo:onControl(0)) $0040)
                                                        Print(16 8 60 1 134 -1 15 140 290)
                                                    )(else
                                                        (if (& (send gEgo:onControl(0)) $0010)
                                                            Print(16 9 60 1 134 -1 15 140 290)
                                                        )(else
                                                            (if (& (send gEgo:onControl(0)) $0400)
                                                                Print(16 10 60 1 134 -1 15 140 290)
                                                            )(else
                                                                (if (& (send gEgo:onControl(0)) $1000)
                                                                    Print(16 11 60 1 134 -1 15 140 290)
                                                                )(else
                                                                    Print(800 1)
                                                                )
                                                            )
                                                        )
                                                    )
                                                )
                                            )
                                        )
                                    )
                                )
                            )
                        )
                    )(else
                        (if (Said('look>'))
                            (if (Said('<in/forest') or Said('/forest<hole'))
                                (if ((send gEgo:inRect(103 115 226 177)))
                                    Print(16 12)
                                )(else
                                    Print(800 1)
                                )
                            )(else
                                (if (Said('/cross'))
                                    Print(16 13)
                                )(else
                                    (if (Said('/forest'))
                                        Print(16 14)
                                    )(else
                                        (if (Said('/hole'))
                                            = local7 1
                                            (while (<= local7 global113)
                                                (if ((== gNewPropX[(= local5 (* (- local7 1) 3))] 16) and (< (egoDist:doit(gNewPropX[(+ local5 1)] gNewPropX[(+ local5 2)])) 20))
                                                    = local8 1
                                                )
                                                ++local7
                                            )
                                            (if (local8)
                                                Print(16 15)
                                            )(else
                                                (if ((send gEgo:inRect(103 115 226 177)))
                                                    Print(16 12)
                                                )(else
                                                    Print(800 1)
                                                )
                                            )
                                            = local8 0
                                        )(else
                                            (if (Said('[<around][/room]'))
                                                Print(16 16)
                                            )
                                        )
                                    )
                                )
                            )
                        )(else
                            (if (Said('dig[/grave,hole]'))
                                (if ((send gEgo:has(15)) and (== 0 (send ((Inv:at(15))):loop())))
                                    (if (> global134 0)
                                        (if (& (send gEgo:onControl(0)) $7fff)
                                            (send gEgo:setScript(digging))
                                            (digging:changeState(1))
                                        )(else
                                            Print(16 17)
                                        )
                                    )(else
                                        Print(16 18)
                                    )
                                )(else
                                    Print(16 19)
                                )
                            )
                        )
                    )
                )
            )(else
                0
            )
    )


    (method (newRoom param1)
        (send gEgo:illegalBits(32768))
        = global189 0
        (if (== (send gEgo:view()) 2)
            (super:newRoom(param1))
        )
    )

)
(instance z1Actions of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                (= seconds 4  )
            )
            (case 1
                (= global189 1 (zTheme:
                    number(20)
                    loop(30000)
                    play()
                )
 (send newAct:
                    show()
                    setCycle(End self)
                )
 ++global186 (send ((View:new())):
                    view(260)
                    posn((send newAct:x()) (send newAct:y()))
                    ignoreActors()
                    addToPic()
                )
  )
            )
            (case 2
                (
                (if (== global105 0)
                    (send newAct:
                        view(261)
                        setCycle(Walk)
                        setAvoider((Avoid:new()))
                        setMotion(Chase gEgo 12 self)
                    )
                )(else
                    (send newAct:
                        view(261)
                        setCycle(Walk)
                        setAvoider((Avoid:new()))
                        setMotion(Wander 50)
                    )
                )
 )
            )
            (case 3
                (
                (if ((send gEgo:has(7)))
                    (self:changeState(4))
                )(else
                    (if (== global105 0)
                        (self:changeState(10))
                    )
                )
 )
            )
            (case 4
                (Print(16 20 134 -1 20) 
                (if (== --global186 0)
                    (zTheme:dispose())
                )
 (send newAct:
                    setMotion(0)
                    view(260)
                    cel(11)
                    setCycle(Beg self)
                )
  )
            )
            (case 5
                ((send newAct:
                    ignoreActors()
                    addToPic()
                )
  )
            )
            (case 10
                (proc0_10() = global105 17 (zTheme:dispose()) (zTheme:
                    number(21)
                    play()
                )
 (send gEgo:
                    view(36)
                    cel(0)
                    setCycle(End self)
                )
 (send newAct:setMotion(Wander)) 
                (if ((send gCast:contains(newAct_2)))
                    (send newAct_2:setMotion(Wander))
                )
 
                (if ((send gCast:contains(newAct_3)))
                    (send newAct_3:setMotion(Wander))
                )
 )
            )
            (case 11
                ((send gEgo:
                    view(46)
                    cel(0)
                    setCycle(End self)
                )
  )
            )
            (case 12
                (= global120 Print(16 21 134 -1 20 172) (send gEgo:
                    view(45)
                    setCycle(Walk)
                    setMotion(Wander)
                )
 = seconds 9  )
            )
            (case 13
                (proc0_23() (zTheme:dispose()) = global127 1 )
            )
        )
    )

)
(instance z2Actions of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                (= seconds 10  )
            )
            (case 1
                (
                (if (not (send gSounds:contains(zTheme)))
                    (zTheme:
                        number(20)
                        loop(30000)
                        play()
                    )
                )
 ++global186 (send newAct_2:
                    show()
                    setCycle(End self)
                )
 (send ((View:new())):
                    view(262)
                    cel(0)
                    posn((send newAct_2:x()) (send newAct_2:y()))
                    ignoreActors()
                    addToPic()
                )
  )
            )
            (case 2
                (
                (if (== global105 0)
                    (send newAct_2:
                        view(263)
                        setCycle(Walk)
                        setAvoider((Avoid:new()))
                        setMotion(Chase gEgo 12 self)
                    )
                )(else
                    (send newAct_2:
                        view(263)
                        setCycle(Walk)
                        setAvoider((Avoid:new()))
                        setMotion(Wander 50)
                    )
                )
 )
            )
            (case 3
                (
                (if ((send gEgo:has(7)))
                    (self:changeState(4))
                )(else
                    (if (== global105 0)
                        (self:changeState(10))
                    )
                )
 )
            )
            (case 4
                ((send newAct_2:
                    setMotion(0)
                    view(262)
                    cel(11)
                    setCycle(Beg self)
                )
 Print(16 20 134 -1 20) 
                (if (== --global186 0)
                    (zTheme:dispose())
                )
 )
            )
            (case 5
                ((send newAct_2:
                    ignoreActors()
                    addToPic()
                )
  )
            )
            (case 10
                (proc0_10() = global105 17 (zTheme:dispose()) (zTheme:
                    number(21)
                    play()
                )
 (send gEgo:
                    view(36)
                    cel(0)
                    setMotion(0)
                    setCycle(End self)
                )
 (send newAct_2:setMotion(Wander)) 
                (if ((send gCast:contains(newAct)))
                    (send newAct:setMotion(Wander))
                )
 
                (if ((send gCast:contains(newAct_3)))
                    (send newAct_3:setMotion(Wander))
                )
 )
            )
            (case 11
                ((send gEgo:
                    view(46)
                    cel(0)
                    setCycle(End self)
                )
  )
            )
            (case 12
                (= global120 Print(16 21 134 -1 20 172) (send gEgo:
                    view(45)
                    setCycle(Walk)
                    setMotion(Wander)
                )
 = seconds 9  )
            )
            (case 13
                (proc0_23() (zTheme:dispose()) = global127 1 )
            )
        )
    )

)
(instance z3Actions of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                (= seconds 15  )
            )
            (case 1
                (
                (if (not (send gSounds:contains(zTheme)))
                    (zTheme:
                        number(20)
                        loop(30000)
                        play()
                    )
                )
 ++global186 (send newAct_3:
                    show()
                    setCycle(End self)
                )
 (send ((View:new())):
                    view(264)
                    posn((send newAct_3:x()) (send newAct_3:y()))
                    ignoreActors()
                    addToPic()
                )
  )
            )
            (case 2
                (
                (if (== global105 0)
                    (send newAct_3:
                        view(265)
                        setCycle(Walk)
                        setAvoider((Avoid:new()))
                        setMotion(Chase gEgo 12 self)
                    )
                )(else
                    (send newAct_3:
                        view(265)
                        setCycle(Walk)
                        setAvoider((Avoid:new()))
                        setMotion(Wander 50)
                    )
                )
 )
            )
            (case 3
                (
                (if ((send gEgo:has(7)))
                    (self:changeState(4))
                )(else
                    (if (== global105 0)
                        (self:changeState(10))
                    )
                )
 )
            )
            (case 4
                (Print(16 20 134 -1 20) 
                (if (== --global186 0)
                    (zTheme:dispose())
                )
 (send newAct_3:
                    setMotion(0)
                    view(264)
                    cel(11)
                    setCycle(Beg self)
                )
  )
            )
            (case 5
                ((send newAct_3:
                    ignoreActors()
                    addToPic()
                )
  )
            )
            (case 10
                (proc0_10() = global105 17 (zTheme:dispose()) (zTheme:
                    number(21)
                    play()
                )
 (send gEgo:
                    view(36)
                    cel(0)
                    setMotion(0)
                    setCycle(End self)
                )
 (send newAct_3:setMotion(Wander)) 
                (if ((send gCast:contains(newAct)))
                    (send newAct:setMotion(Wander))
                )
 
                (if ((send gCast:contains(newAct_2)))
                    (send newAct_2:setMotion(Wander))
                )
 )
            )
            (case 11
                ((send gEgo:
                    view(46)
                    cel(0)
                    setCycle(End self)
                )
  )
            )
            (case 12
                (= global120 Print(16 21 134 -1 20 172) (send gEgo:
                    view(45)
                    setCycle(Walk)
                    setMotion(Wander)
                )
 = seconds 9  )
            )
            (case 13
                (proc0_23() (zTheme:dispose()) = global127 1 )
            )
        )
    )

)
(instance holeActions of Script
    (properties)

    (method (cue)
        (var temp0)
        = temp0 (send newProp:cel())
        ++temp0
        (if (>= state 0)
            (send newProp:cel(temp0))
        )(else
            ++state
        )
    )

)
(instance digging of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 1
                (proc0_10() (send gEgo:
                    view(47)
                    loop(& (send gEgo:loop()) $0001)
                    cel(0)
                    setCycle(End self)
                )
 = newProp (Prop:new()) (send newProp:
                    posn(
                        (if (== (send gEgo:loop()) 1)
                            - (send gEgo:x()) 16
                        )(else
                            + (send gEgo:x()) 16
                        )
 + (send gEgo:y()) 4)
                    view(528)
                    cel(0)
                    setPri(0)
                    ignoreActors()
                    init()
                )
  )
            )
            (case 2
                ((send gEgo:
                    cel(0)
                    setCycle(End self)
                )
  )
            )
            (case 3
                (
                (if (>= global113 5)
                    Print(16 22)
                    (send gEgo:
                        view(2)
                        setCycle(Walk)
                    )
                    (send ((Inv:at(15))):loop(1))
                    (self:changeState(0))
                    proc0_11()
                )(else
                    (send gEgo:
                        cel(0)
                        setCycle(End self)
                    )
                )
 )
            )
            (case 4
                ((send gEgo:
                    cel(0)
                    setCycle(End self)
                )
  )
            )
            (case 5
                ((holeActions:cue()) 
                (if (< (send newProp:cel()) 6)
                    (self:changeState(2))
                )(else
                    (self:changeState(6))
                )
 )
            )
            (case 6
                (= gNewPropX[(= local5 (* (- ++global113 1) 3))] gGNewPropX = gNewPropX[(+ local5 1)] (send newProp:x()) = gNewPropX[(+ local5 2)] (send newProp:y()) (send gEgo:
                    view(2)
                    setCycle(Walk)
                )
 proc0_11() 
                (if (((& (send gEgo:onControl(0)) $0008) and (== global134 2)) and (send ((Inv:at(13))):ownedBy(88)))
                    (send newProp:
                        setLoop(1)
                        cel(2)
                    )
                    Print(16 23 134 -1 20 166)
                    (send ((Inv:at(13))):moveTo(16))
                    (send gEgo:setScript(getItems))
                    (getItems:changeState(1))
                    (send gEgo:get(13))
                    (send gGame:changeScore(3))
                )(else
                    (if (((& (send gEgo:onControl(0)) $0100) and (== global134 1)) and (send ((Inv:at(12))):ownedBy(88)))
                        (send newProp:
                            setLoop(1)
                            cel(1)
                        )
                        Print(16 24 134 -1 20 166)
                        (send gEgo:setScript(getItems))
                        (getItems:changeState(1))
                        (send gGame:changeScore(3))
                        (send gEgo:get(12))
                    )(else
                        (if (((& (send gEgo:onControl(0)) $0004) and (== global134 4)) and (send ((Inv:at(29))):ownedBy(88)))
                            (send newProp:
                                setLoop(1)
                                cel(3)
                            )
                            Print(16 25 134 -1 20 166)
                            (send ((Inv:at(29))):moveTo(16))
                            (send gEgo:setScript(getItems))
                            (getItems:changeState(1))
                            (send gEgo:get(29))
                            (send gGame:changeScore(3))
                        )(else
                            proc0_11()
                            Print(16 26 134 -1 20)
                        )
                    )
                )
 )
            )
        )
    )

)
(instance getItems of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 1
                (proc0_10() proc0_3(gEgo newProp) (send gEgo:
                    view(21)
                    cel(0)
                    setCycle(End self)
                )
  )
            )
            (case 2
                ((send newProp:
                    setLoop(0)
                    cel(6)
                )
 = global182 1 (send gEgo:setCycle(Beg self))  )
            )
            (case 3
                (= cycles 6  )
            )
            (case 4
                (Print(16 27) (send gEgo:
                    view(2)
                    setCycle(Walk)
                )
 proc0_11() )
            )
        )
    )

)
(instance egoDist of Code
    (properties)

    (method (doit param1 param2)
        (var temp0)
        = temp0 Sqrt((+ (* Abs((- (send gEgo:y()) param2)) Abs((- (send gEgo:y()) param2))) (* Abs((- (send gEgo:x()) param1)) Abs((- (send gEgo:x()) param1)))))
    )

)
