(version 2)
(include "sci.sh")
(use "Main")
(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)
        = gOrigX (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
                    )
                = pEventModifiers 0
                (send pEvent:
                    type(pEventType)
                    message(pEventMessage)
                    modifiers(pEventModifiers)
                )
            )
            (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 gOldCast:firstTrue(#perform findNoun))) or (= temp4 (send gOldFeatures:firstTrue(#perform findNoun)))) or (= temp4 (send gOldATPs:firstTrue(#perform findNoun)))))
                    (send temp4:doVerb((send ((send gIconBar:curIcon)):message)))
                )(else
                    = temp4 (send gIconBar:findIcon(pEventMessage))
                    (if (temp4)
                        (send gIconBar:select((= temp4 (send gIconBar:findIcon(pEventMessage)))))
                        (send gGame:setCursor((send temp4:cursor)))
                    )
                )
            )(else
                (if (& pEventType $0040)
                    (if (gOldDH and (send gOldDH:handleEvent(pEvent)))
                        return 1
                    )(else
                        (if ((((((gIconBar and (== (send gIconBar:curIcon) (send gIconBar:walkIconItem))) or not gIconBar) and alterEgo) and controls) and (send gOldCast: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 gOldKH) and (send gOldKH:handleEvent(pEvent)))
                        return 1
                    )(else
                        (if (((& pEventType $0003) and gOldMH) and (send gOldMH:handleEvent(pEvent)))
                            return 1
                        )
                    )
                )
            )
        )
        (if (gIconBar)
            (send gIconBar:handleEvent(pEvent))
        )
        = pEventType (send pEvent:type)
        = pEventMessage (send pEvent:message)
        (if (input and (& pEventType $4000))
            (if (((& pEventType $1000) and gOldWH) and (send gOldWH:handleEvent(pEvent)))
                return 1
            )(else
                (if ((((& pEventType $1000) and (send gOldCast:contains(alterEgo))) and controls) and (send alterEgo:handleEvent(pEvent)))
                    return 1
                )(else
                    (if (global34)
                        (OnMeAndLowY:init())
                        (send gOldCast:eachElementDo(#perform OnMeAndLowY pEvent))
                        (send gOldFeatures:eachElementDo(#perform OnMeAndLowY pEvent))
                        (send gOldATPs:eachElementDo(#perform OnMeAndLowY pEvent))
                        (if ((OnMeAndLowY:theObj) and (send ((OnMeAndLowY:theObj)):handleEvent(pEvent)))
                            return 1
                        )
                    )(else
                        (if ((send gOldCast:handleEvent(pEvent)))
                            return 1
                        )(else
                            (if ((send gOldFeatures:handleEvent(pEvent)))
                                return 1
                            )(else
                                (if ((send gOldATPs:handleEvent(pEvent)))
                                    return 1
                                )
                            )
                        )
                    )
                )
            )
            (if (not (send pEvent:claimed) and (send gRegions:handleEvent(pEvent)))
                return 1
            )
        )
        (if (pEventType)
            (if ((send gGame: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
    )

)
