(include "sci.sh")
(use "Main")
(use "Print")
(use "Polygon")
(use "Sound")
(use "SRDialog")
(use "Cycle")
(use "Sounds")
(use "InvI")
(use "User")
(use "Obj")
(script 994)



(procedure (localproc_0e32 param1)
    (var temp0, temp1[40], temp41[40], temp81, temp82[40], temp122[65290], temp65412[5])
    = temp81 Memory(1 150)
    = temp0 1
    DeviceInfo(0 global29 @temp1)
    DeviceInfo(1 @temp41)
    (if (DeviceInfo(3 @temp41) and (DeviceInfo(2 @temp1 @temp41) or not DeviceInfo(6 (send gSQ5:name))))
        GetMessage(0 994 6 0 0 1 @temp82)
        GetMessage(0 994 7 0 0 1 @temp122)
        GetMessage(0 994 8 0 0 1 @temp65412)
        Format(temp81 @temp82 
            (if (param1)
                @temp122
            )(else
                @temp65412
            )
 @temp1)
        Load(rsFONT gFont)
        DeviceInfo(4)
        GetMessage(0 994 2 0 0 1 @temp82)
        GetMessage(0 994 4 0 0 1 @temp122)
        GetMessage(0 994 5 0 0 1 @temp65412)
        (if (== (= temp0 
            (if (param1)
                (Print:
                    font(0)
                    addText(temp81)
                    addButton(1 @temp82 0 40)
                    addButton(0 @temp122 30 40)
                    addButton(2 @temp65412)
                    init()
                )
            )(else
                (Print:
                    font(0)
                    addText(temp81)
                    addButton(1 @temp82 0 40)
                    init()
                )
            )
) 2)
            = temp0 proc990_0(global29)
        )
    )
    Memory(3 temp81)
    return temp0
)


(instance cast of EventHandler
    (properties)
)
(instance features of EventHandler
    (properties)
)
(instance theDoits of EventHandler
    (properties)
)
(class Sounds of EventHandler
    (properties
        elements 0
        size 0
    )

    (method (pause param1)
        (self:eachElementDo(96 mayPause 
            (if (paramTotal)
                param1
            )(else
                1
            )
))
    )

)
(instance mayPause of Code
    (properties)

    (method (doit param1 param2)
        (if (not & (send param1:flags) $0001)
            (send param1:pause(param2))
        )
    )

)
(instance regions of EventHandler
    (properties)
)
(instance addToPics of EventHandler
    (properties)

    (method (doit)
        (self:eachElementDo(96 aTOC))
        AddToPic(elements)
    )

)
(instance timers of Set
    (properties)
)
(instance mouseDownHandler of EventHandler
    (properties)
)
(instance keyDownHandler of EventHandler
    (properties)
)
(instance directionHandler of EventHandler
    (properties)
)
(instance walkHandler of EventHandler
    (properties)
)
(class Cue of Obj
    (properties
        cuee 0
        cuer 0
        register 0
    )

    (method (doit)
        (send gNewSet:delete(self))
        (if ((send gNewSet:isEmpty()))
            (send gNewSet:dispose())
            = gNewSet 0
        )
        (send cuee:cue(register cuer))
        (self:dispose())
    )

)
(instance aTOC of Code
    (properties)

    (method (doit param1)
        (var temp0, temp1)
        (if (not & (send param1:signal) $4000)
            = temp0 (+ (send gEgo:xStep) (/ CelWide((send gEgo:view) 2 0) 2))
            = temp1 (* (send gEgo:yStep) 2)
            (send gRoom:addObstacle((send ((Polygon:new())):
                    init((- (send param1:brLeft) temp0) (- CoordPri(1 CoordPri((send param1:y))) temp1) (+ (send param1:brRight) temp0) (- CoordPri(1 CoordPri((send param1:y))) temp1) (+ (send param1:brRight) temp0) (+ (send param1:y) temp1) (- (send param1:brLeft) temp0) (+ (send param1:y) temp1))
                    yourself()
                )
))
        )
    )

)
(class Game of Obj
    (properties
        script 0
        printLang 1
        _detailLevel 3
        panelObj 0
        panelSelector 0
        handsOffCode 0
        handsOnCode 0
    )

    (method (init)
        Motion
        Sound
        (send (= gCast cast):add())
        (send (= gFeatures features):add())
        (send (= gSounds Sounds):add())
        (send (= gRegions regions):add())
        (send (= gAddToPics addToPics):add())
        (send (= gTimers timers):add())
        (send (= gTheDoits theDoits):add())
        (send (= gMouseDownHandler mouseDownHandler):add())
        (send (= gKeyDownHandler keyDownHandler):add())
        (send (= gDirectionHandler directionHandler):add())
        (send (= gWalkHandler walkHandler):add())
        = gNewEventHandler 0
        = global29 GetSaveDir()
        (Inv:init())
        (if (not gUser)
            = gUser User
        )
        (send gUser:init())
    )


    (method (doit)
        (var newEvent, thePanelObj, thePanelSelector)
        (if (panelObj)
            = thePanelObj panelObj
            = thePanelSelector panelSelector
            = panelObj (= panelSelector 0)
            proc999_7(thePanelObj thePanelSelector)
        )
        = gCycleCnt (+ global86 GetTime())
        (if (gNewEventHandler)
            (while (gNewEventHandler)
                (send gNewEventHandler:eachElementDo(57))
                (if ((send ((= newEvent (Event:new()))):type) and gNewEventHandler)
                    (send gNewEventHandler:firstTrue(133 newEvent))
                )
                (send newEvent:dispose())
                = gCycleCnt (+ global86 GetTime())
                (send gSounds:eachElementDo(180))
            )
        )
        (if (global92)
            (send global92:eachElementDo(57))
            (if (not gDialog)
                (if ((send ((= newEvent (Event:new()))):type) and global92)
                    (send global92:firstTrue(133 newEvent))
                )
                (send newEvent:dispose())
                = gCycleCnt (+ global86 GetTime())
                return 
            )
        )
        (send gSounds:eachElementDo(180))
        (send gTimers:eachElementDo(57))
        (if (gDialog and (send gDialog:check()))
            (send gDialog:dispose())
        )
        Animate((send gCast:elements) 1)
        (if (global37)
            = global37 0
            (send gCast:eachElementDo(253))
        )
        (if (gNewSet)
            (send gNewSet:eachElementDo(57))
        )
        (if (script)
            (send script:doit())
        )
        (send gRegions:eachElementDo(57))
        (if (gNewEventHandler)
            return 
        )
        (if (== gTheGModNum gModNum)
            (send gUser:doit())
        )
        (send gTheDoits:doit())
        (if (<> gTheGModNum gModNum)
            (self:newRoom(gTheGModNum))
        )
        (send gTimers:eachElementDo(81))
        GameIsRestarting(0)
    )


    (method (play)
        = gSQ5 self
        = global29 GetSaveDir()
        (self:
            setCursor(global21 1)
            init()
        )
        (self:setCursor(global20 1))
        (while (not global4)
            (self:doit())
        )
    )


    (method (replay)
        (var temp0)
        (if (gPEvent)
            (send gPEvent:dispose())
        )
        (if (gDialog)
            (send gDialog:dispose())
        )
        (send gCast:eachElementDo(96 RU))
        (send gSQ5:setCursor(global21 1))
        = temp0 
            (if (not proc999_5((send gRoom:style) -1 11 12 13 14))
                (send gRoom:style)
            )(else
                100
            )
        DrawPic((send gRoom:curPic) temp0 1)
        (if (<> global36 -1)
            DrawPic(global36 100 0)
        )
        (send gAddToPics:doit())
        (if (not (send gUser:canControl()) and not (send gUser:canInput()))
            (send gSQ5:setCursor(global21))
        )(else
            (if (gSq5IconBar and (send gSq5IconBar:curIcon))
                (send gSQ5:setCursor((send ((send gSq5IconBar:curIcon)):cursor)))
            )(else
                (send gSQ5:setCursor(global20))
            )
        )
        (SL:doit())
        DoSound(2)
        (send gSounds:pause(0))
        = global86 (- gCycleCnt GetTime())
        (while (not global4)
            (self:doit())
        )
    )


    (method (newRoom theGModNum)
        (var temp0[5], temp5)
        (send gAddToPics:
            eachElementDo(111)
            eachElementDo(81)
            release()
        )
        (send gFeatures:
            eachElementDo(96 fDC)
            release()
        )
        (send gCast:
            eachElementDo(111)
            eachElementDo(81)
        )
        (send gTimers:eachElementDo(81))
        (send gRegions:
            eachElementDo(96 DNKR)
            release()
        )
        (send gTheDoits:release())
        Animate(0)
        = gGModNum gModNum
        = gModNum theGModNum
        = gTheGModNum theGModNum
        FlushResources(theGModNum)
        (self:startRoom(gModNum))
        (while ((send (= temp5 (Event:new(3))):type))
            (send temp5:dispose())
        )
        (send temp5:dispose())
    )


    (method (startRoom param1)
        (if (global14)
            SetDebug()
        )
        (send gRegions:addToFront((= gRoom ScriptID(param1))))
        (send gRoom:init())
    )


    (method (restart)
        (if (gDialog)
            (send gDialog:dispose())
        )
        RestartGame()
    )


    (method (restore param1)
        (var temp0[20], temp20, temp21, temp22[100], temp122[5], temp127[65380])
        (if (not ValidPath(global29))
            GetMessage(0 994 9 0 0 1 @temp22)
            Format(@temp127 @temp22 global29)
            (Print:
                font(0)
                addText(@temp127)
                init()
            )
            proc990_0(global29)
        )
        Load(rsFONT global23)
        ScriptID(990)
        = temp21 (self:setCursor(global20))
        (send gSounds:pause(1))
        (if (localproc_0e32(1))
            (if (gDialog)
                (send gDialog:dispose())
            )
            (if (<> (= temp20 (Restore:doit(rest param1))) -1)
                (self:setCursor(global21 1))
                (if (CheckSaveGame(name temp20 global27))
                    RestoreGame(name temp20 global27)
                )(else
                    GetMessage(0 994 3 0 0 1 @temp22)
                    GetMessage(0 994 2 0 0 1 @temp122)
                    (Print:
                        font(0)
                        addText(@temp22)
                        addButton(1 @temp122 0 40)
                        init()
                    )
                    (self:setCursor(temp21 HaveMouse()))
                )
            )
            localproc_0e32(0)
        )
        (send gSounds:pause(0))
    )


    (method (save)
        (var temp0[20], temp20, temp21, temp22[100], temp122[5], temp127[65380])
        (if (not ValidPath(global29))
            GetMessage(0 994 9 0 0 1 @temp22)
            Format(@temp127 @temp22 global29)
            (Print:
                font(0)
                addText(@temp127)
                init()
            )
            proc990_0(global29)
        )
        Load(rsFONT global23)
        ScriptID(990)
        = temp21 (self:setCursor(global20))
        (send gSounds:pause(1))
        (if (localproc_0e32(1))
            (if (gDialog)
                (send gDialog:dispose())
            )
            (if (<> (= temp20 (Save:doit(@temp0))) -1)
                = temp21 (self:setCursor(global21 1))
                (if (not SaveGame(name temp20 @temp0 global27))
                    GetMessage(0 994 1 0 0 1 @temp22)
                    GetMessage(0 994 2 0 0 1 @temp122)
                    (Print:
                        font(0)
                        addText(@temp22)
                        addButton(1 @temp122 0 40)
                        init()
                    )
                )
                (self:setCursor(temp21 HaveMouse()))
            )
            localproc_0e32(0)
        )
        (send gSounds:pause(0))
    )


    (method (changeScore param1)
        = global15 (+ global15 param1)
        (SL:doit())
    )


    (method (handleEvent pEvent)
        (if ((send pEvent:claimed))
            1
        )(else
            (if (script and (send script:handleEvent(pEvent)))
                1
            )(else
                (if (& (send pEvent:type) $4000)
                    (self:pragmaFail())
                )
            )
        )
        (send pEvent:claimed)
    )


    (method (showMem)
        (var temp0[64])
        Format(@temp0 "Free Heap: %u Bytes\nLargest ptr: %u Bytes\nFreeHunk: %u KBytes\nLargest hunk: %u Bytes" MemoryInfo(1) MemoryInfo(0) >> MemoryInfo(3) $0006 MemoryInfo(2))
        (Print:
            addText(@temp0)
            init()
        )
    )


    (method (setCursor theGSaveCursor_2 param2 param3 param4 param5 param6)
        (var theGSaveCursor)
        = theGSaveCursor gSaveCursor
        (if (IsObject(theGSaveCursor_2))
            = gSaveCursor theGSaveCursor_2
            (send theGSaveCursor_2:init())
        )(else
            SetCursor(theGSaveCursor_2 0 0)
        )
        (if (> paramTotal 1)
            SetCursor(param2)
            (if (> paramTotal 2)
                SetCursor(param3 param4)
                (if (> paramTotal 4)
                    SetCursor(theGSaveCursor_2 0 0 param5 param6)
                )
            )
        )
        return theGSaveCursor
    )


    (method (notify)
    )


    (method (setScript param1 param2)
        (if (script)
            (send script:dispose())
        )
        (if (param1)
            (send param1:init(self rest param2))
        )
    )


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


    (method (quitGame param1)
        (if (not paramTotal or param1)
            = global4 1
        )
    )


    (method (masterVolume param1)
        (if (paramTotal)
            DoSound(0 param1)
        )(else
            DoSound(0)
        )
    )


    (method (detailLevel the_detailLevel)
        (if (paramTotal)
            = _detailLevel the_detailLevel
            (send gCast:eachElementDo(304))
        )
        return _detailLevel
    )


    (method (pragmaFail)
    )


    (method (handsOff param1)
        (if (handsOffCode)
            (send handsOffCode:doit(rest param1))
        )(else
            (User:
                canControl(0)
                canInput(0)
            )
            (if (IsObject(gEgo))
                (send gEgo:setMotion(0))
            )
        )
    )


    (method (handsOn param1)
        (if (handsOnCode)
            (send handsOnCode:doit(rest param1))
        )(else
            (User:
                canControl(1)
                canInput(1)
            )
        )
    )

)
(class Rgn of Obj
    (properties
        script 0
        number 0
        modNum -1
        noun 0
        timer 0
        keep 0
        initialized 0
    )

    (method (init)
        (if (not initialized)
            = initialized 1
            (if (not (send gRegions:contains(self)))
                (send gRegions:addToEnd(self))
            )
            (super:init())
        )
    )


    (method (doit)
        (if (script)
            (send script:doit())
        )
    )


    (method (dispose)
        (send gRegions:delete(self))
        (if (IsObject(script))
            (send script:dispose())
        )
        (if (IsObject(timer))
            (send timer:
                dispose()
                delete()
            )
        )
        (send gSounds:eachElementDo(181 self))
        DisposeScript(number)
    )


    (method (handleEvent pEvent)
        (if ((send pEvent:claimed))
            1
        )(else
            (if (& (send pEvent:type) $0040)
                0
            )(else
                (if (not 
                (if (script and ((send script:handleEvent(pEvent)) or 1))
                    (send pEvent:claimed)
                )
)
                    (send pEvent:claimed((self:doVerb((send pEvent:message)))))
                )
            )
        )
        (send pEvent:claimed)
    )


    (method (doVerb param1)
        (var temp0)
        (if (== modNum -1)
            = modNum gModNum
        )
        return 
            (if (GetMessage(0 modNum noun param1 0 1))
                (send gTestMessager:say(noun param1 0 0 0 modNum))
                1
            )(else
                0
            )
    )


    (method (setScript param1 param2)
        (if (IsObject(script))
            (send script:dispose())
        )
        (if (param1)
            (send param1:init(self rest param2))
        )
    )


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


    (method (newRoom)
    )


    (method (notify)
    )

)
(class Rm of Rgn
    (properties
        script 0
        number 0
        modNum -1
        noun 0
        timer 0
        keep 0
        initialized 0
        picture 0
        style -1
        horizon 0
        controls 0
        north 0
        east 0
        south 0
        west 0
        curPic 0
        picAngle 0
        vanishingX 160
        vanishingY 0
        obstacles 0
        inset 0
    )

    (method (init)
        = number gModNum
        = gPicAngle picAngle
        (if (picture)
            (self:drawPic(picture))
        )
        (self:reflectPosn((send gUser:alterEgo) (send ((send gUser:alterEgo)):edgeHit)))
        (send ((send gUser:alterEgo)):edgeHit(0))
    )


    (method (doit)
        (var temp0)
        (if (script)
            (send script:doit())
        )
        (if (= temp0 (self:edgeToRoom((send ((send gUser:alterEgo)):edgeHit))))
            (self:newRoom((= temp0 (self:edgeToRoom((send ((send gUser:alterEgo)):edgeHit))))))
        )
    )


    (method (dispose)
        (if (obstacles)
            (send obstacles:dispose())
        )
        (super:dispose())
    )


    (method (handleEvent pEvent)
        (if (inset and (send inset:handleEvent(pEvent)))
        )(else
            (super:handleEvent(pEvent))
        )
        (send pEvent:claimed)
    )


    (method (newRoom theGTheGModNum)
        (send gRegions:
            delete(self)
            eachElementDo(385 theGTheGModNum)
            addToFront(self)
        )
        = gTheGModNum theGTheGModNum
        (super:newRoom(theGTheGModNum))
    )


    (method (setRegions param1)
        (var temp0, temp1, temp2)
        = temp0 0
        (while (< temp0 paramTotal)
            (send (= temp2 ScriptID((= temp1 param1[temp0]))):number(temp1))
            (send gRegions:add(temp2))
            (if (not (send temp2:initialized))
                (send temp2:init())
            )
            ++temp0
        )
    )


    (method (drawPic theCurPic param2)
        (if (gAddToPics)
            (send gAddToPics:
                eachElementDo(111)
                release()
            )
        )
        = curPic theCurPic
        = global36 -1
        DrawPic(theCurPic 
            (if (== paramTotal 2)
                param2
            )(else
                (if (<> style -1)
                    style
                )(else
                    100
                )
            )
 1)
    )


    (method (overlay param1 param2)
        = global36 param1
        DrawPic(param1 
            (if (== paramTotal 2)
                param2
            )(else
                (if (<> style -1)
                    style
                )(else
                    100
                )
            )
 0)
    )


    (method (addObstacle param1 param2)
        (if (not IsObject(obstacles))
            = obstacles (List:new())
        )
        (send obstacles:add(param1 rest param2))
    )


    (method (reflectPosn param1 param2)
        (switch (param2)
            (case 1
                ((send param1:y(188))  )
            )
            (case 4
                ((send param1:x((- 319 (send param1:xStep))))  )
            )
            (case 3
                ((send param1:y((+ horizon (send param1:yStep))))  )
            )
            (case 2
                ((send param1:x(1)) )
            )
        )
    )


    (method (edgeToRoom param1)
        (switch (param1)
            (case 1
                (north  )
            )
            (case 2
                (east  )
            )
            (case 3
                (south  )
            )
            (case 4
                (west )
            )
        )
    )


    (method (roomToEdge param1)
        (switch (param1)
            (case north
                (1  )
            )
            (case south
                (3  )
            )
            (case east
                (2  )
            )
            (case west
                (4 )
            )
        )
    )


    (method (setInset param1 param2 param3)
        (if (inset)
            (send inset:dispose())
        )
        (if (paramTotal and param1)
            (send param1:init(
                (if (>= paramTotal 2)
                    param2
                )(else
                    0
                )
 self 
                (if (>= paramTotal 3)
                    param3
                )(else
                    0
                )
))
        )
    )

)
(class SL of Obj
    (properties
        state 0
        code 0
    )

    (method (doit)
        (var temp0)
        (if (code)
            (send code:doit((= temp0 Memory(1 150))))
            DrawStatus(
                (if (state)
                    temp0
                )(else
                    0
                )
)
            Memory(3 temp0)
        )
    )


    (method (enable)
        = state 1
        (self:doit())
    )


    (method (disable)
        = state 0
        (self:doit())
    )

)
(instance RU of Code
    (properties)

    (method (doit param1)
        (var temp0)
        (if ((send param1:underBits))
            (send param1:
                underBits(0)
                signal(= temp0 (& (= temp0 (| (= temp0 (send param1:signal)) $0001)) $fffb))
            )
        )
    )

)
(instance DNKR of Code
    (properties)

    (method (doit param1)
        (if (not (send param1:keep))
            (send param1:dispose())
        )
    )

)
(instance fDC of Code
    (properties)

    (method (doit param1)
        (if ((send param1:respondsTo(81)))
            (send param1:
                signal(& (send param1:signal) $ffdf)
                dispose()
                delete()
            )
        )(else
            (send param1:dispose())
        )
    )

)
