(include "sci.sh")
(use "Main")
(use "User")
(use "Obj")
(script 996)



(instance uEvt of Event
    (properties)

    (method (new)
        = type (= message (= modifiers (= y (= x (= claimed (= port 0))))))
        GetEvent(32767 self)
        return self
    )

)
(class User of Obj
    (properties
        alterEgo 0
        input 0
        controls 0
        prevDir 0
        x -1
        y -1
        mapKeyToDir 1
        curEvent 0
    )

    (method (init)
        = curEvent uEvt
    )


    (method (doit)
        (send curEvent:new())
        (self:handleEvent(curEvent))
    )


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


    (method (handleEvent pEvent)
        (var pEventType, pEventMessage, pEventModifiers, temp3, temp4)
        = gPEventX (send pEvent:x)
        = gPEventY (send pEvent:y)
        = pEventType (send pEvent:type)
        = pEventModifiers (send pEvent:modifiers)
        (if (pEventType)
            = gPEvent pEvent
            (if (mapKeyToDir)
                MapKeyToDir(pEvent)
            )
            (if (== pEventType 256)
                = pEventType 4
                = pEventMessage 
                    (if (& pEventModifiers $0003)
                        27
                    )(else
                        13
                    )
                (send pEvent:
                    type(pEventType)
                    message(pEventMessage)
                    modifiers(= pEventModifiers 0)
                )
            )
            (if (global75 and (send global75:handleEvent(pEvent)))
                return 1
            )
            (if (global92 and (send global92:handleEvent(pEvent)))
                return 1
            )
            (send pEvent:localize())
            = pEventType (send pEvent:type)
            = pEventMessage (send pEvent:message)
            (if (& pEventType $0080)
                (if ((== pEventMessage 1) and (((= temp4 (send gCast:firstTrue(96 findNoun))) or (= temp4 (send gFeatures:firstTrue(96 findNoun)))) or (= temp4 (send gAddToPics:firstTrue(96 findNoun)))))
                    (send temp4:doVerb((send ((send gSq5IconBar:curIcon)):message)))
                )(else
                    (if (= temp4 (send gSq5IconBar:findIcon(pEventMessage)))
                        (send gSq5IconBar:select((= temp4 (send gSq5IconBar:findIcon(pEventMessage)))))
                        (send gSQ5:setCursor((send temp4:cursor)))
                    )
                )
            )(else
                (if (& pEventType $0040)
                    (if (gDirectionHandler and (send gDirectionHandler:handleEvent(pEvent)))
                        return 1
                    )(else
                        (if ((((((gSq5IconBar and (== (send gSq5IconBar:curIcon) (send gSq5IconBar:walkIconItem))) or not gSq5IconBar) and alterEgo) and controls) and (send gCast:contains(alterEgo))) and (send alterEgo:handleEvent(pEvent)))
                            return 1
                        )(else
                            (if ((gPseudoMouse and (not (& pEventType $0004) or (<> pEventMessage 0))) and (send gPseudoMouse:handleEvent(pEvent)))
                                return 1
                            )
                        )
                    )
                )(else
                    (if (((& pEventType $0004) and gKeyDownHandler) and (send gKeyDownHandler:handleEvent(pEvent)))
                        return 1
                    )(else
                        (if (((& pEventType $0003) and gMouseDownHandler) and (send gMouseDownHandler:handleEvent(pEvent)))
                            return 1
                        )
                    )
                )
            )
        )
        (if (gSq5IconBar)
            (send gSq5IconBar:handleEvent(pEvent))
        )
        = pEventType (send pEvent:type)
        = pEventMessage (send pEvent:message)
        (if (input and (& pEventType $4000))
            (if (((& pEventType $1000) and gWalkHandler) and (send gWalkHandler:handleEvent(pEvent)))
                return 1
            )(else
                (if ((((& pEventType $1000) and (send gCast:contains(alterEgo))) and controls) and (send alterEgo:handleEvent(pEvent)))
                    return 1
                )(else
                    (if (global34)
                        (OnMeAndLowY:init())
                        (send gCast:eachElementDo(96 OnMeAndLowY pEvent))
                        (send gFeatures:eachElementDo(96 OnMeAndLowY pEvent))
                        (send gAddToPics:eachElementDo(96 OnMeAndLowY pEvent))
                        (if ((OnMeAndLowY:theObj) and (send ((OnMeAndLowY:theObj)):handleEvent(pEvent)))
                            return 1
                        )
                    )(else
                        (if ((send gCast:handleEvent(pEvent)))
                            return 1
                        )(else
                            (if ((send gFeatures:handleEvent(pEvent)))
                                return 1
                            )(else
                                (if ((send gAddToPics:handleEvent(pEvent)))
                                    return 1
                                )
                            )
                        )
                    )
                )
            )
            (if (not (send pEvent:claimed) and (send gRegions:handleEvent(pEvent)))
                return 1
            )
        )
        (if (pEventType)
            (if ((send gSQ5:handleEvent(pEvent)))
                return 1
            )(else
                (if (global92 and (send global92:handleEvent(pEvent)))
                    return 1
                )
            )
        )
        return 0
    )


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

)
(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)
        )
    )

)
(instance findNoun of Code
    (properties)

    (method (doit param1 param2)
        return == (send param1:noun) param2
    )

)
