(include "sci.sh")
(use "Main")
(use "Class_255_0")
(use "Cycle")
(use "Obj")
(script 998)



(class PV of Obj
    (properties
        view 0
        loop 0
        cel 0
        x 0
        y 0
        priority 0
        signal 0
    )

    (method (init)
        (send ((View:new())):
            view(view)
            loop(loop)
            cel(cel)
            x(x)
            y(y)
            priority(priority)
            signal(| signal $8021)
            init()
        )
    )


    (method (showSelf)
        Print(name 164 view loop cel)
    )

)
(class View of Obj
    (properties
        y 0
        x 0
        yStep 2
        view 0
        loop 0
        cel 0
        priority 0
        underBits 0
        signal 257
        nsTop 0
        nsLeft 0
        nsBottom 0
        nsRight 0
        lsTop 0
        lsLeft 0
        lsBottom 0
        lsRight 0
        brTop 0
        brLeft 0
        brBottom 0
        brRight 0
    )

    (method (init)
        (if (not (send gCast:contains(self)))
            = lsRight (= lsBottom (= lsLeft (= lsTop 0)))
            = signal (& signal $ff77)
        )
        BaseSetter(self)
        (send gCast:add(self))
    )


    (method (dispose)
        (self:
            startUpd()
            hide()
        )
        = signal (| signal $8000)
    )


    (method (showSelf)
        Print(name 164 view loop cel)
    )


    (method (posn theX theY)
        = x theX
        = y theY
        BaseSetter(self)
        (self:forceUpd())
    )


    (method (stopUpd)
        = signal (| signal $0001)
        = signal (& signal $fffd)
    )


    (method (forceUpd)
        = signal (| signal $0040)
    )


    (method (startUpd)
        = signal (| signal $0002)
        = signal (& signal $fffe)
    )


    (method (setPri thePriority)
        (if (== paramTotal 0)
            = signal (| signal $0010)
        )(else
            (if (== thePriority -1)
                = signal (& signal $ffef)
            )(else
                = priority thePriority
                = signal (| signal $0010)
            )
        )
        (self:forceUpd())
    )


    (method (setLoop theLoop)
        (if (== paramTotal 0)
            = signal (| signal $0800)
        )(else
            (if (== theLoop -1)
                = signal (& signal $f7ff)
            )(else
                = loop theLoop
                = signal (| signal $0800)
            )
        )
        (self:forceUpd())
    )


    (method (setCel param1)
        (if (== paramTotal 0)
            = signal (| signal $1000)
        )(else
            (if (== param1 -1)
                = signal (& signal $efff)
            )(else
                = signal (| signal $1000)
                = cel 
                    (if (>= param1 (self:lastCel()))
                        (self:lastCel())
                    )(else
                        param1
                    )
            )
        )
        (self:forceUpd())
    )


    (method (ignoreActors param1)
        (if ((== 0 paramTotal) or (== param1 1))
            = signal (| signal $4000)
        )(else
            = signal (& signal $bfff)
        )
    )


    (method (hide)
        = signal (| signal $0008)
    )


    (method (show)
        = signal (& signal $fff7)
    )


    (method (delete)
        (if (& signal $8000)
            (if (& signal $0020)
                (send gAddToPics:add((send ((PV:new())):
                        view(view)
                        loop(loop)
                        cel(cel)
                        x(x)
                        y(y)
                        priority(priority)
                        signal(signal)
                        yourself()
                    )
))
            )
            = signal (& signal $7fff)
            (send gCast:delete(self))
            (if (underBits)
                UnLoad(133 underBits)
                = underBits 0
            )
            (super:dispose())
        )
    )


    (method (handleEvent)
    )


    (method (addToPic)
        (self:
            signal(| signal $8021)
            init()
        )
    )


    (method (lastCel)
        return - NumCels(self) 1
    )


    (method (isExtra param1)
        (var temp0)
        = temp0 (& signal $0200)
        (if (> paramTotal 0)
            (if (param1)
                = signal (| signal $0200)
            )(else
                = signal (& signal $fdff)
            )
        )
        return temp0
    )

)
(class Prop of View
    (properties
        y 0
        x 0
        yStep 2
        view 0
        loop 0
        cel 0
        priority 0
        underBits 0
        signal 0
        nsTop 0
        nsLeft 0
        nsBottom 0
        nsRight 0
        lsTop 0
        lsLeft 0
        lsBottom 0
        lsRight 0
        brTop 0
        brLeft 0
        brBottom 0
        brRight 0
        cycleSpeed 0
        script 0
        cycler 0
        timer 0
    )

    (method (doit)
        (var temp0)
        (if (& signal $8000)
            return 
        )
        (if (script)
            (send script:doit())
        )
        (if ((& signal $0004) and not (& signal $0002))
            return 
        )
        (if (cycler)
            (send cycler:doit())
        )
    )


    (method (delete)
        (if (& signal $8000)
            (self:setScript(0))
            (self:setCycle(0))
            (if (timer)
                (send timer:dispose())
            )
            (super:delete())
        )
    )


    (method (handleEvent pEvent)
        (if (script)
            (send script:handleEvent(pEvent))
        )
    )


    (method (setCycle param1 param2)
        (if (cycler)
            (send cycler:dispose())
        )
        (if (param1)
            (self:startUpd())
            = cycler (send param1:new())
            (send cycler:init(self rest param2))
        )(else
            = cycler 0
        )
    )


    (method (setScript theScript)
        (if (script)
            (send script:dispose())
        )
        (if (= script theScript)
            (send (= script theScript):init(self))
        )
    )


    (method (cue)
        (if (script)
            (send script:cue())
        )
    )

)
(class Act of Prop
    (properties
        y 0
        x 0
        yStep 2
        view 0
        loop 0
        cel 0
        priority 0
        underBits 0
        signal 0
        nsTop 0
        nsLeft 0
        nsBottom 0
        nsRight 0
        lsTop 0
        lsLeft 0
        lsBottom 0
        lsRight 0
        brTop 0
        brLeft 0
        brBottom 0
        brRight 0
        cycleSpeed 0
        script 0
        cycler 0
        timer 0
        illegalBits 32768
        xLast 0
        yLast 0
        xStep 3
        heading 0
        moveSpeed 0
        blocks 0
        baseSetter 0
        mover 0
        looper 0
        viewer 0
        avoider 0
    )

    (method (init)
        (super:init())
        = xLast x
        = yLast y
    )


    (method (doit)
        (var temp0)
        (if (& signal $8000)
            return 
        )
        (if (script)
            (send script:doit())
        )
        (if ((& signal $0004) and not (& signal $0002))
            return 
        )
        (if (viewer)
            (send viewer:doit(self))
        )
        = signal (& signal $fbff)
        (if (avoider)
            (send avoider:doit())
        )(else
            (if (mover)
                (send mover:doit())
            )
        )
        (if (cycler)
            (send cycler:doit())
            (if (not (self:canBeHere()))
                (self:findPosn())
            )
        )
        = xLast x
        = yLast y
    )


    (method (posn theXLast theYLast)
        (super:posn(theXLast theYLast))
        = xLast theXLast
        = yLast theYLast
        (if (not (self:canBeHere()))
            (self:findPosn())
        )
    )


    (method (delete)
        (if (& signal $8000)
            (if (<> mover -1)
                (self:setMotion(0))
            )
            (self:setAvoider(0))
            (if (baseSetter)
                (send baseSetter:dispose())
                = baseSetter 0
            )
            (if (looper)
                (send looper:dispose())
                = looper 0
            )
            (if (viewer)
                (send viewer:dispose())
                = viewer 0
            )
            (if (blocks)
                (send blocks:dispose())
                = blocks 0
            )
            (super:delete())
        )
    )


    (method (setMotion param1 param2)
        (var temp0[40])
        (if (mover and (<> mover -1))
            (send mover:dispose())
        )
        (if (param1)
            (self:startUpd())
            = mover 
                (if (& (send param1:{-info-}()) $8000)
                    (send param1:new())
                )(else
                    param1
                )
            (send mover:init(self rest param2))
        )(else
            = mover 0
        )
    )


    (method (setAvoider param1)
        (if (avoider)
            (send avoider:dispose())
        )
        = avoider 
            (if (IsObject(param1) and (& (send param1:{-info-}()) $8000))
                (send param1:new())
            )(else
                param1
            )
        (if (avoider)
            (send avoider:
                client(self)
                init()
            )
        )
    )


    (method (ignoreHorizon param1)
        (if ((== 0 paramTotal) or (== param1 1))
            = signal (| signal $2000)
        )(else
            = signal (& signal $dfff)
        )
    )


    (method (observeControl param1)
        (var temp0)
        = temp0 0
        (while (< temp0 paramTotal)
            = illegalBits (| illegalBits param1[temp0])
            ++temp0
        )
    )


    (method (ignoreControl param1)
        (var temp0)
        = temp0 0
        (while (< temp0 paramTotal)
            = illegalBits (& illegalBits bnot param1[temp0])
            ++temp0
        )
    )


    (method (observeBlocks param1)
        (if (not blocks)
            = blocks (Set:new())
        )
        (send blocks:add(rest param1))
    )


    (method (ignoreBlocks param1)
        (send blocks:delete(rest param1))
        (if ((send blocks:isEmpty()))
            (send blocks:dispose())
            = blocks 0
        )
    )


    (method (isStopped)
        return 
            (if ((not not IsObject(mover) and (send mover:triedToMove())) and (== x xLast))
                == y yLast
            )(else
                0
            )
    )


    (method (isBlocked)
        return & signal $0400
    )


    (method (findPosn)
        (var temp0, temp1, theX, theY, temp4)
        = theX x
        = theY y
        = temp4 0
        = temp1 1
        (while (not temp4)
            = temp0 0
            (while (not temp4 and (< temp0 8))
                (switch (temp0)
                    (case 0
                        (= x (- theX temp1) = y theY  )
                    )
                    (case 1
                        (= x (+ theX temp1)  )
                    )
                    (case 2
                        (= x (- theX temp1) = y (- theY temp1)  )
                    )
                    (case 3
                        (= x (+ theX temp1) = y (- theY temp1)  )
                    )
                    (case 4
                        (= x (- theX temp1) = y (+ theY temp1)  )
                    )
                    (case 5
                        (= x (+ theX temp1) = y (+ theY temp1)  )
                    )
                    (case 6
                        (= x theX = y (- theY temp1)  )
                    )
                    (case 7
                        (= y (+ theY temp1) )
                    )
                )
                = temp4 
                    (if (not ((self:canBeHere()) and (self:onControl())) or (& signal $2000))
                    )(else
                        (>= y (send global2:horizon()))
                    )
                ++temp0
            )
            ++temp1
        )
        (self:posn(x y))
    )


    (method (inRect param1 param2 param3 param4)
        return 
            (if (((<= param1 x) and (< x param3)) and (<= param2 y))
                < y param4
            )(else
                0
            )
    )


    (method (onControl param1)
        (if (paramTotal and param1)
            OnControl(x y)
        )(else
            OnControl(brLeft brTop brRight brBottom)
        )
    )


    (method (distanceTo param1)
        GetDistance(x y (send param1:x()) (send param1:y()))
    )


    (method (canBeHere)
        (if (baseSetter)
            (send baseSetter:doit(self))
        )(else
            BaseSetter(self)
        )
        (if (not (CanBeHere(self (send gCast:elements())) and (((& signal $2000) or not IsObject(global2)) or (>= y (send global2:horizon())))) or (== blocks 0))
        )(else
            (send blocks:allTrue(120 self))
        )
    )


    (method (setStep theXStep theYStep)
        (if ((>= paramTotal 1) and (<> theXStep -1))
            = xStep theXStep
        )
        (if ((>= paramTotal 2) and (<> theYStep -1))
            = yStep theYStep
        )
        (if ((mover and (<> -1 mover)) and (send mover:isMemberOf(MoveTo)))
            (self:setMotion(MoveTo (send mover:x()) (send mover:y()) (send mover:client())))
        )
    )


    (method (setDirection param1)
        (var theX, theY, temp2, temp3)
        (if (== param1 0)
            (self:setMotion(0))
            return 
        )
        (if (mover)
            (if (== param1 
            (if (== heading 0)
                1
            )(else
                (if (< heading 90)
                    2
                )(else
                    (if (== heading 90)
                        3
                    )(else
                        (if (< heading 180)
                            4
                        )(else
                            (if (== heading 180)
                                5
                            )(else
                                (if (< heading 270)
                                    6
                                )(else
                                    (if (== heading 270)
                                        7
                                    )(else
                                        8
                                    )
                                )
                            )
                        )
                    )
                )
            )
)
                return 
            )
        )
        = theX x
        = theY y
        = temp2 (* xStep 400)
        = temp3 (* yStep 400)
        (switch (param1)
            (case 1
                (= theY (- theY temp3)  )
            )
            (case 2
                (= theX (+ theX temp2) = theY (- theY temp3)  )
            )
            (case 3
                (= theX (+ theX temp2)  )
            )
            (case 4
                (= theX (+ theX temp2) = theY (+ theY temp3)  )
            )
            (case 5
                (= theY (+ theY temp3)  )
            )
            (case 6
                (= theX (- theX temp2) = theY (+ theY temp3)  )
            )
            (case 7
                (= theX (- theX temp2)  )
            )
            (case 8
                (= theX (- theX temp2) = theY (- theY temp3) )
            )
        )
        (self:setMotion(MoveTo theX theY))
    )

)
(class Ego of Act
    (properties
        y 0
        x 0
        yStep 2
        view 0
        loop 0
        cel 0
        priority 0
        underBits 0
        signal 8192
        nsTop 0
        nsLeft 0
        nsBottom 0
        nsRight 0
        lsTop 0
        lsLeft 0
        lsBottom 0
        lsRight 0
        brTop 0
        brLeft 0
        brBottom 0
        brRight 0
        cycleSpeed 0
        script 0
        cycler 0
        timer 0
        illegalBits 32768
        xLast 0
        yLast 0
        xStep 3
        heading 0
        moveSpeed 0
        blocks 0
        baseSetter 0
        mover 0
        looper 0
        viewer 0
        avoider 0
        edgeHit 0
    )

    (method (init)
        (super:init())
        (if (not cycler)
            (self:setCycle(Walk))
        )
    )


    (method (doit)
        (super:doit())
        = edgeHit 
            (if (<= x 0)
                4
            )(else
                (if (<= y (send global2:horizon()))
                    1
                )(else
                    (if (>= x 319)
                        2
                    )(else
                        (if (>= y 189)
                            3
                        )(else
                            0
                        )
                    )
                )
            )
    )


    (method (get param1)
        (var temp0)
        = temp0 0
        (while (< temp0 paramTotal)
            (send ((send gInv:at(param1[temp0]))):moveTo(self))
            ++temp0
        )
    )


    (method (put param1 param2)
        (if ((self:has(param1)))
            (send ((send gInv:at(param1))):moveTo(
                (if (== paramTotal 1)
                    -1
                )(else
                    param2
                )
))
        )
    )


    (method (has param1)
        (var temp0)
        (if (= temp0 (send gInv:at(param1)))
            (send (= temp0 (send gInv:at(param1))):ownedBy(self))
        )
    )

)
(class Blk of Obj
    (properties
        top 0
        left 0
        bottom 0
        right 0
    )

    (method (doit param1)
        return 
            (if (((<= (send param1:brBottom()) top) or (> (send param1:brTop()) bottom)) or (< (send param1:brRight()) left))
            )(else
                >= (send param1:brLeft()) right
            )
    )

)
(class Cage of Blk
    (properties
        top 0
        left 0
        bottom 0
        right 0
    )

    (method (doit param1)
        return 
            (if (((>= (send param1:brTop()) top) and (>= (send param1:brLeft()) left)) and (<= (send param1:brBottom()) bottom))
                <= (send param1:brRight()) right
            )(else
                0
            )
    )

)
