(version 2)
(include "sci.sh")
(exports
    0 bug
)
(use "Main")
(use "Class_255_0")
(use "Wander")
(use "Follow")
(use "Chase")
(use "Avoid")
(use "Sound")
(use "Cycle")
(use "Feature")
(use "Obj")
(script 511)


(local
    local0
    bugX
    bugY
    local3
    local4

)
(instance public bug of Act
    (properties)

    (method (init)
        (super:init())
        (self:setScript(scorp))
    )


    (method (delete)
        (super:delete())
        DisposeScript(511)
    )

)
(instance zap of Sound
    (properties)
)
(instance bugBlock of Blk
    (properties)
)
(instance scorp of Script
    (properties)

    (method (init param1)
        (super:init(rest param1))
        Load(132 90)
    )


    (method (doit)
        (super:doit())
        (if (<> global11 global13)
            return 
        )
        (if ((send global5:contains(bug)))
            (if (& OnControl(2 (- (bug:x) 13) (- (bug:y) 1) (+ (bug:x) 13) (bug:y)) global591)
                (bug:posn(bugX (+ bugY 2)))
                (if (local0)
                    (bug:
                        setAvoider(Avoid)
                        setMotion(Chase gEgo 5 self)
                    )
                )(else
                    (bug:
                        setAvoider(0)
                        setMotion(Wander 200)
                    )
                )
            )
            (if (((== global104 0) and (== global119 0)) and (< (send gEgo:distanceTo(bug)) 150))
                proc255_0(511 0)
                = global119 1
            )
            (if (<> OnControl(1 (bug:x) (bug:y)) 0)
                = bugX (bug:x)
                = bugY (bug:y)
            )
            (if ((< state 2) or (== state 6))
                (if (<> global104 0)
                    (if (== local0 1)
                        = local0 0
                        (self:changeState(6))
                    )
                )(else
                    (if (<> local0 1)
                        = state 1
                        (bug:
                            setAvoider(0)
                            setAvoider(Avoid)
                            setMotion(Chase gEgo 5 self)
                        )
                        = local0 1
                    )
                )
            )
        )
    )


    (method (changeState newState)
        (switch (= state newState)
            (case 0
                (bug:
                    view(71)
                    posn(Random(1 300) Random(160 180))
                    illegalBits(-2)
                    ignoreControl(4)
                    setStep(3 2)
                    setLoop(-1)
                    setCycle(Walk)
                    setAvoider(Avoid)
                    setMotion(Chase gEgo 5 self)
                )
                (switch (global11)
                    (case 42
                        = local4 0
                        (bug:ignoreControl(8192 4096))
                    )
                    (case 44
                        = local4 0
                        (bug:ignoreControl(8192))
                    )
                    (case 45
                        = local4 82
                    )
                    (case 46
                        = local4 80
                        (bug:ignoreControl(2048))
                    )
                    (case 47
                        = local4 105
                        (bug:ignoreControl(16))
                    )
                    (case 48
                        = local4 86
                    )
                    (case 50
                        = local4 55
                        (bug:ignoreControl(8))
                    )
                    (case 51
                        = local4 87
                        (bug:ignoreControl(8))
                    )
                    (case 52
                        = local4 97
                    )
                )
                (bugBlock:
                    top(0)
                    bottom(local4)
                    right(319)
                    left(0)
                )
                (bug:observeBlocks(bugBlock))
                (if (not global119 and (== global104 0))
                    proc255_0(511 0)
                    = global119 1
                )
                (self:changeState(1))
                = local0 1
            )
            (case 2
                (if (<> global11 global13)
                    return 
                )
                (if (<> global104 0)
                    = local0 0
                    (self:changeState(6))
                    return 
                )
                proc0_2()
                (send gEgo:setMotion(0))
                (if (< (bug:x) (send gEgo:x))
                    (bug:
                        setLoop(6)
                        cel(255)
                    )
                )(else
                    (bug:
                        setLoop(7)
                        cel(255)
                    )
                )
                (bug:
                    setCycle(End self)
                    setAvoider(0)
                    setAvoider(Avoid)
                    setMotion(Follow gEgo 1)
                )
            )
            (case 3
                (if (<> global104 0)
                    = local0 0
                    (self:changeState(6))
                    return 
                )
                proc0_2()
                (send gEgo:setMotion(0))
                (zap:
                    number(90)
                    priority(10)
                    play()
                )
                (bug:
                    setMotion(0)
                    setPri(14)
                    setLoop(8)
                    setAvoider(0)
                    cel(0)
                )
                = cycles 3
            )
            (case 4
                (if (<> global104 0)
                    = local0 0
                    (self:changeState(6))
                    return 
                )
                (send gEgo:
                    view(16)
                    setLoop(0)
                    cel(& (send gEgo:loop) $0001)
                )
                (bug:
                    setPri(-1)
                    setLoop(-1)
                    setCycle(Walk)
                    setAvoider(0)
                    setMotion(Wander 200)
                )
                proc0_10()
                proc255_0(511 1)
                = seconds 5
            )
            (case 5
                proc0_17(901 0 12 17)
            )
            (case 6
                (bug:
                    setLoop(-1)
                    setCycle(Walk)
                    setAvoider(0)
                    setMotion(Wander 200)
                )
                = local0 0
            )
        )
    )

)
