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


(local
    newAct
    local1
    local2
    local3

)
(instance public regTroll of Rgn
    (properties)

    (method (init)
        (if (initialized)
            return 
        )
        = keep 1
        Load(rsVIEW 67)
        Load(rsVIEW 4)
        Load(rsVIEW 640)
        Load(rsVIEW 190)
        (super:init())
        = global189 1
        = local2 0
        = global126 0
        = local1 0
        = local3 0
        (trollCaveMusic:init())
        (trollMusic:init())
        (caughtMusic:init())
        (doMusic:cue())
    )


    (method (doit)
        (super:doit())
        (if (== script gotchaScript)
            return 
        )
        (if (not proc0_22() and not (& (send gEgo:onControl(0)) $0002))
            (send gEgo:dispose())
            proc0_20()
            Print(605 1)
            (self:setScript(gotchaScript))
            return 
        )
        (if (not (send gCast:contains(newAct)) and (== global126 1))
            (trollScript:changeState(1))
        )
        (if (not local1)
            = local1 1
            (if (((not global126 and (< Random(0 100) 50)) and (<> gGNewPropX 76)) and (<> gGNewPropX 73))
                (trollScript:start(0))
                (self:setScript(trollScript))
            )
        )
    )


    (method (dispose)
        (if (== keep 0)
            = global189 0
            (super:dispose())
        )
    )


    (method (handleEvent pEvent)
        (var gInvSaidMe, temp1, temp2)
        (super:handleEvent(pEvent))
        (if ((send pEvent:claimed()))
            return 1
        )
        return 
            (if (== (send pEvent:type()) 128)
                = temp2 
                    (if (proc0_22())
                        (send gEgo:has(3))
                    )(else
                        0
                    )
                (if ((send gEgo:has(3)) and Said('/chandelier,lantern[<oil]>'))
                    (if (Said('extinguish,(turn<off)'))
                        (self:notify(0))
                    )(else
                        (if (Said('>,ignite,(turn<on)'))
                            (self:notify(1))
                        )
                    )
                )(else
                    (if (Said('look>'))
                        (if (Said('<out[/cave]'))
                            Print(605 2)
                        )(else
                            (if (temp2 and Said('[<in,at]/cave[<troll,dark]'))
                                Print(605 3)
                            )(else
                                (if (temp2 and (Said('/dirt') or Said('<down')))
                                    Print(605 4)
                                )(else
                                    (if (temp2 and Said('/passageway'))
                                        Print(605 5)
                                    )(else
                                        (if (Said('/troll'))
                                            Print(605 6)
                                        )(else
                                            (if ((send gInv:saidMe()))
                                                (send pEvent:claimed(0))
                                            )(else
                                                Print(605 7)
                                                (send pEvent:claimed(1))
                                            )
                                        )
                                    )
                                )
                            )
                        )
                    )(else
                        (if (Said('climb/boulder') or Said('get/boulder'))
                            Print(605 8)
                        )(else
                            (if (Said('find/troll'))
                                Print(605 6)
                            )(else
                                (if ((send gCast:contains(newAct)))
                                    (if (Said('converse/troll'))
                                        Print(605 9)
                                    )(else
                                        (if (Said('kill/troll'))
                                            Print(605 10)
                                        )(else
                                            (if (Said('get,capture/troll'))
                                                Print(605 11)
                                            )(else
                                                (if (Said('kiss/troll') or Said('kiss[/!*]'))
                                                    Print(605 12)
                                                )(else
                                                    (if ((Said('deliver>') and (= gInvSaidMe (send gInv:saidMe()))) and (send gEgo:has((send gInv:indexOf((= gInvSaidMe (send gInv:saidMe())))))))
                                                        Print(605 13)
                                                    )
                                                )
                                            )
                                        )
                                    )
                                )
                            )
                        )
                    )
                )
            )(else
                0
            )
    )


    (method (newRoom param1)
        = local1 0
        = local3 0
        proc0_23()
        (if ((== param1 77) or (== param1 70))
            (trollCaveMusic:keep(0))
            (trollMusic:keep(0))
            (caughtMusic:keep(0))
            (send gSounds:eachElementDo(172))
        )(else
            (if (not (send gCast:contains(newAct)))
                (doMusic:cue())
            )
        )
        (super:newRoom(param1))
    )


    (method (notify param1)
        (switch (param1)
            (case 2
                ((send gEgo:view(4)) (theLight:dispose())  )
            )
            (case 3
                ((send gEgo:view(67)) (theLight:init())  )
            )
            (case 1
                ((send gEgo:view(67)) (theLight:init()) proc0_20() proc0_22(1)  )
            )
            (case 0
                ((send gEgo:view(4)) (theLight:dispose()) 
                (if (local3)
                    (send gEgo:dispose())
                    proc0_20()
                )(else
                    (if (& (send gEgo:onControl(0)) $0002)
                        proc0_22(0)
                    )(else
                        (send gEgo:dispose())
                        proc0_20()
                        Print(605 0)
                        (self:setScript(gotchaScript))
                    )
                )
 )
            )
            (case 4
                ((trollMusic:dispose()) 
                (if (IsObject(newAct))
                    (send newAct:
                        setMotion(0)
                        setScript(0)
                    )
                )
 )
            )
        )
    )

)
(instance trollScript of Script
    (properties)

    (method (doit)
        (if (((send gCast:contains(newAct)) and (< (self:state()) 5)) and (< (send gEgo:distanceTo(newAct)) 5))
            (self:changeState(5))
            return 
        )
        (super:doit())
    )


    (method (changeState newState)
        (switch (= state newState)
            (case 0
                (
                (if (not local2)
                    = seconds 2
                )
 )
            )
            (case 1
                (
                (if (not global126 and ((((== gGNewPropX 71) or (== gGNewPropX 72)) or (== gGNewPropX 74)) or (== gGNewPropX 75)))
                    Print(605 14 134 200 50 172)
                    = global126 1
                    (send (= newAct (Act:new())):
                        view(190)
                        setAvoider(Avoid)
                        illegalBits(0)
                        xStep(5)
                        yStep(2)
                        init()
                        setCycle(Walk)
                    )
                    (switch (gGNewPropX)
                        (case 71
                            (send newAct:posn(230 96))
                        )
                        (case 72
                            (send newAct:posn(190 180))
                        )
                        (case 74
                            (send newAct:posn(73 166))
                        )
                        (case 75
                            (send newAct:posn(107 89))
                        )
                    )
                    (trollCaveMusic:
                        keep(0)
                        dispose()
                    )
                    (trollMusic:
                        dispose()
                        loop(1)
                        play()
                    )
                )(else
                    (if ((<> global126 1) or (== gGNewPropX 73))
                        return 
                    )
                    (send (= newAct (Act:new())):
                        view(190)
                        setAvoider(Avoid)
                        setCycle(Walk)
                        illegalBits(0)
                        ignoreActors(1)
                        xStep(6)
                        yStep(2)
                        init()
                    )
                    (if (<= (send gEgo:heading()) 90)
                        (send newAct:posn((- (send gEgo:x()) 30) (+ (send gEgo:y()) 10)))
                    )(else
                        (if (<= (send gEgo:heading()) 180)
                            (send newAct:posn((- (send gEgo:x()) 10) (- (send gEgo:y()) 30)))
                        )(else
                            (if (<= (send gEgo:heading()) 270)
                                (send newAct:posn((+ (send gEgo:x()) 30) (- (send gEgo:y()) 10)))
                            )(else
                                (send newAct:posn((+ (send gEgo:x()) 10) (+ (send gEgo:y()) 30)))
                            )
                        )
                    )
                    (if (== gGNewPropX 76)
                        (switch (Random(0 1))
                            (case 0
                                (send newAct:
                                    xStep(20)
                                    yStep(4)
                                )
                                (send gSounds:eachElementDo(172))
                                (trollCaveMusic:
                                    keep(0)
                                    dispose()
                                )
                                (trollMusic:
                                    dispose()
                                    loop(1)
                                    play()
                                )
                            )
                            (case 1
                                (send newAct:hide())
                                = local2 1
                                (self:changeState(7))
                                return 
                            )
                        )
                    )(else
                        (trollCaveMusic:
                            keep(0)
                            dispose()
                        )
                        (trollMusic:
                            dispose()
                            loop(1)
                            play()
                        )
                    )
                    proc0_20()
                )
 
                (if (not local2)
                    (self:cue())
                    return 
                )
 )
            )
            (case 2
                (
                (if ((send gCast:contains(newAct)))
                    (send newAct:
                        moveSpeed(1)
                        cycleSpeed(1)
                        setMotion(Chase gEgo 55 self)
                    )
                )(else
                    (self:cue())
                )
 )
            )
            (case 3
                (Print(605 15 50 4 134 200 20 172) (self:cue())  )
            )
            (case 4
                (
                (if ((send gCast:contains(newAct)))
                    (send newAct:setMotion(Chase gEgo 4 self))
                )(else
                    (self:cue())
                )
 )
            )
            (case 5
                ((gotchaScript:start(1)) (send global2:setScript(gotchaScript)) )
            )
        )
    )

)
(instance gotchaScript of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                (proc0_10() proc0_23() 
                (if (== (trollCaveMusic:state()) 0)
                    (trollCaveMusic:
                        keep(0)
                        dispose()
                    )
                )
 (trollMusic:play()) = seconds 5  )
            )
            (case 1
                (proc0_10() (trollCaveMusic:
                    keep(0)
                    dispose()
                )
 (trollMusic:dispose()) (caughtMusic:play()) proc0_23() Print(605 16 134 -1 20) = local3 1 (regTroll:notify(0)) Print(605 17 134 -1 20) = seconds 4  )
            )
            (case 2
                (= global127 1 )
            )
        )
    )

)
(instance theLight of Prop
    (properties
        view 640
    )

    (method (init)
        (self:
            posn((send gEgo:x()) - (send gEgo:y()) 3)
            ignoreActors(1)
            setCycle(Fwd)
            priority(- (send gEgo:priority()) 1)
        )
        (super:init())
    )


    (method (doit)
        (self:
            posn((send gEgo:x()) - (send gEgo:y()) 3)
            priority(- (send gEgo:priority()) 1)
        )
        (super:doit())
    )

)
(instance trollCaveMusic of Sound
    (properties
        number 66
        keep 1
    )
)
(instance caughtMusic of Sound
    (properties
        number 38
        keep 1
    )
)
(instance trollMusic of Sound
    (properties
        number 37
        priority 1
        loop -1
        keep 1
    )

    (method (play param1)
        (trollCaveMusic:dispose())
        (super:play(rest param1))
    )

)
(instance doMusic of Script
    (properties)

    (method (changeState newState)
        (switch (= state newState)
            (case 0
                ((Timer:setReal(self 2))  )
            )
            (case 1
                ((trollCaveMusic:
                    loop(1)
                    playMaybe()
                )
 )
            )
        )
    )

)
