(version 2)
(include "sci.sh")
(exports
    0 PestRegion
)
(use "Main")
(use "Class_255_0")
(use "Follow")
(use "Chase")
(use "Avoid")
(use "Cycle")
(use "Game")
(use "Feature")
(use "Obj")
(script 503)


(local
    local0
    local1

)
(instance guard of Act
    (properties)
)
(instance beam of View
    (properties)
)
(instance blast of Act
    (properties)
)
(instance public PestRegion of Rgn
    (properties)

    (method (init)
        (super:init())
        Load(128 92)
        Load(132 44)
    )


    (method (doit)
        (super:doit())
        (if ((== global105 1) and (<= --global121 0))
            = global105 0
            = global120 2
            (send gEgo:view(0))
        )
    )


    (method (handleEvent pEvent)
        (if ((send pEvent:claimed))
            return 1
        )
        return 
            (if (== (send pEvent:type) 128)
                (if (Said('look>'))
                    (if ((Said('/area') or Said('/around')) or Said('[<around][/!*]'))
                        proc255_0(503 0)
                    )
                )(else
                    (if (Said('turn<on/belt') or Said('activate/belt'))
                        (if (== global120 2)
                            proc255_0(503 1)
                        )(else
                            (if (== global120 1)
                                proc255_0(503 2)
                            )(else
                                proc255_0(503 3)
                                = global120 1
                                = global105 1
                                (send gEgo:view(123))
                            )
                        )
                    )(else
                        (if (Said('turn<off/belt') or Said('deactivate/belt'))
                            (if (== global120 2)
                                proc255_0(503 4)
                            )(else
                                (if (== global120 0)
                                    proc255_0(503 5)
                                )(else
                                    proc255_0(503 3)
                                    = global105 0
                                    = global120 1
                                    (send gEgo:view(0))
                                )
                            )
                        )(else
                            (if (Said('converse/guard'))
                                = gSeconds 3
                                (self:notify())
                            )
                        )
                    )
                )
            )(else
                0
            )
    )


    (method (notify)
        (self:setScript(guardActions))
    )

)
(instance guardActions of Script
    (properties)

    (method (doit)
        (super:doit())
        (if (((== global122 2) and (== global105 1)) and (>= state 2))
            proc255_0(503 6)
            = global122 4
            (guard:setMotion(0))
        )
        (if ((== global122 4) and (== global105 0))
            = global122 2
            = state 1
            (guard:setMotion(Chase gEgo 250))
        )
    )


    (method (changeState newState)
        (switch (= state newState)
            (case 0
                proc255_0(503 7)
                = seconds gSeconds
            )
            (case 1
                = global122 2
                = local1 Random((- (send gEgo:y) 20) (+ (send gEgo:y) 20))
                (if (< (send gEgo:y) (+ (send global2:horizon) 2))
                    = local1 (+ (send global2:horizon) 2)
                )
                (if (< Random(1 10000) 5000)
                    = local0 326
                )(else
                    = local0 -7
                )
                (guard:
                    view(593)
                    setCycle(Walk)
                    illegalBits(-32768)
                    setAvoider(Avoid)
                    posn(local0 local1)
                    setMotion(Chase gEgo 250 self)
                    init()
                )
            )
            (case 2
                (if ((< (guard:x) 3) or (> (guard:x) 316))
                    --state
                    (guard:setMotion(Follow gEgo 5))
                    = seconds 1
                )(else
                    (if ((<> global122 4) and (== global105 0))
                        proc255_0(503 8)
                        (guard:
                            setMotion(0)
                            setLoop(
                                (if (== (guard:loop) 0)
                                    2
                                )(else
                                    3
                                )
)
                            cel(255)
                            setCycle(End self)
                        )
                    )
                )
            )
            (case 3
                (send gLongSong:
                    number(44)
                    loop(1)
                    priority(2)
                    play()
                )
                (guard:
                    setLoop(
                        (if (== (guard:loop) 2)
                            4
                        )(else
                            5
                        )
)
                    cel(255)
                    setCycle(End self)
                )
            )
            (case 4
                proc0_2()
                (blast:
                    view(593)
                    setLoop(
                        (if (== (guard:loop) 0)
                            6
                        )(else
                            7
                        )
)
                    cel(255)
                    setCycle(End)
                    setStep(30 10)
                    ignoreActors()
                    illegalBits(0)
                    setPri((send gEgo:priority))
                    x(
                        (if (== (guard:loop) 4)
                            + (guard:x) 3
                        )(else
                            - (guard:x) 3
                        )
)
                    y(- (guard:y) 16)
                    init()
                )
                proc0_10()
                (blast:setMotion(MoveTo (send gEgo:x) (- (send gEgo:y) 18) self))
            )
            (case 5
                (blast:dispose())
                (send gEgo:
                    view(92)
                    setLoop(2)
                    cel(0)
                    setCycle(Fwd)
                )
                = seconds 7
            )
            (case 6
                proc0_17(901 0 7 999)
            )
        )
    )

)
