(include "sci.sh")
(use "Main")
(use "PolyPath")
(use "Cycle")
(use "PicView")
(use "Obj")
(script 996)


(local
    local0[23]
    local23

)
(instance uEvt of Event
    (properties)
)
(class User of Obj
    (properties
        alterEgo 0
        input 0
        controls 0
        echo 32
        prevDir 0
        prompt "Enter input"
        inputLineAddr 0
        x -1
        y -1
        mapKeyToDir 1
        curEvent 0
        verbMessager 0
    )

    (method (init param1 param2)
        = inputLineAddr 
            (if (paramTotal)
                param1
            )(else
                @local0
            )
        = local23 
            (if (== paramTotal 2)
                param2
            )(else
                45
            )
        = curEvent uEvt
    )


    (method (doit)
        (send curEvent:
            type(0)
            message(0)
            modifiers(0)
            y(0)
            x(0)
            claimed(0)
            port(0)
        )
        GetEvent(32767 curEvent)
        (self:handleEvent(curEvent))
    )


    (method (canControl theControls)
        (if (paramTotal)
            = controls theControls
            = prevDir 0
        )
        return controls
    )


    (method (getInput)
        (var temp0[2])
    )


    (method (said)
        (var temp0)
    )


    (method (handleEvent pEvent)
        (var pEventType, temp1)
        = gPEventX (send pEvent:x)
        = gPEventY (send pEvent:y)
        (if ((send pEvent:type))
            = gPEvent pEvent
            (if (mapKeyToDir)
                MapKeyToDir(pEvent)
            )
            (if (== (send pEvent:type) 256)
                (send pEvent:
                    type(4)
                    message(
                        (if (& (send pEvent:modifiers) $0003)
                            27
                        )(else
                            13
                        )
)
                    modifiers(0)
                )
            )
            = pEventType (send pEvent:type)
            (if (global88)
                (send global88:handleEvent(pEvent pEventType))
            )
            (send pEvent:localize())
            (if (& (send pEvent:type) $0040)
                (if (not (((gPseudoMouse and (send gPseudoMouse:handleEvent(pEvent))) or ((alterEgo and controls) and (send alterEgo:handleEvent(pEvent)))) or (gSq4DirectionHandler and (send gSq4DirectionHandler:handleEvent(pEvent)))) and gIconBar)
                    (send gIconBar:handleEvent(pEvent))
                )
            )(else
                (if (== pEventType 4)
                    (if (gSq4KeyDownHandler)
                        (send gSq4KeyDownHandler:handleEvent(pEvent))
                    )
                )(else
                    (if ((== pEventType 1) and gSq4MouseDownHandler)
                        (send gSq4MouseDownHandler:handleEvent(pEvent))
                    )
                )
            )
        )
        (if (not (send pEvent:claimed))
            (if (gIconBar)
                (send gIconBar:handleEvent(pEvent))
            )
            (if ((== (send pEvent:type) 16384) and input)
                (if (((== (send pEvent:message) 0) and controls) and (send alterEgo:handleEvent(pEvent)))
                    1
                )(else
                    (if (global34)
                        (OnMeAndLowY:init())
                        (send gCast:eachElementDo(99 OnMeAndLowY pEvent))
                        (send gFeatures:eachElementDo(99 OnMeAndLowY pEvent))
                        (if ((OnMeAndLowY:theObj))
                            (send ((OnMeAndLowY:theObj)):handleEvent(pEvent))
                        )
                    )(else
                        (if ((send gCast:handleEvent(pEvent)))
                            1
                        )(else
                            (if ((send gFeatures:handleEvent(pEvent)))
                                1
                            )
                        )
                    )
                )
                (if ((send pEvent:claimed))
                    1
                )(else
                    (if ((send gRegions:handleEvent(pEvent)))
                        1
                    )
                )
            )
            (if ((send pEvent:type) and not (send pEvent:claimed))
                (send gGame:handleEvent(pEvent))
            )
        )
    )


    (method (canInput theInput)
        (if (paramTotal)
            = input theInput
        )
        return input
    )

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

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


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


    (method (handleEvent pEvent)
        (var pEventMessage)
        (if (script)
            (send script:handleEvent(pEvent))
        )
        (if ((send pEvent:claimed) or not (send gCast:contains(self)))
            1
        )(else
            (if (& (send pEvent:type) $0040)
                (if ((== (= pEventMessage (send pEvent:message)) 0) and (& (send pEvent:type) $0004))
                    (send pEvent:claimed)
                    return 
                )
                (if ((== pEventMessage (User:prevDir)) and IsObject(mover))
                    = pEventMessage 0
                )
                (User:prevDir(pEventMessage))
                (self:setDirection(pEventMessage))
                (send pEvent:claimed(1))
            )(else
                (if (& (send pEvent:type) $4000)
                    (if (== (send pEvent:message) 0)
                        (self:setMotion(
                            (if (global87)
                                PolyPath
                            )(else
                                MoveTo
                            )
 (send pEvent:x) (send pEvent:y)))
                        (User:prevDir(0))
                        (send pEvent:claimed(1))
                    )(else
                        (super:handleEvent(pEvent))
                    )
                )(else
                    (super:handleEvent(pEvent))
                )
            )
        )
        (send pEvent:claimed)
    )


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


    (method (put param1 param2)
        (var temp0)
        (if ((self:has(param1)))
            (send (= temp0 (send gInv:at(param1))):moveTo(
                (if (== paramTotal 1)
                    -1
                )(else
                    param2
                )
))
            (if (gIconBar and (== (send gIconBar:curInvIcon) temp0))
                (send ((send gIconBar:
                    curInvIcon(0)
                    useIconItem
                )
):cursor(999))
                (send gGame:setCursor(999 1))
            )
        )
    )


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

)
(class OnMeAndLowY of Code
    (properties
        theObj 0
        lastY -1
    )

    (method (init)
        = theObj 0
        = lastY -1
    )


    (method (doit theTheObj param2)
        (if ((send theTheObj:onMe(param2)) and (> (send theTheObj:y) lastY))
            = lastY (send ((= theObj theTheObj)):y)
        )
    )

)
