(include "sci.sh")
(use "Main")
(use "Class_255_0")
(use "SysWindow")
(use "IconI")
(use "Obj")
(script 995)


(local
    local0[3]
    local3
    theGInvFirst
    local5

)
(procedure (localproc_01a2 param1 param2)
    (var temp0, temp1, temp2, temp3, temp4, temp5, temp6, temp7, gInvFirst, temp9, temp10, temp11, temp12, temp13, temp14, temp15, temp16, temp17, temp18, temp19, temp20)
    = temp5 0
    = temp4 temp5
    = temp3 temp4
    = temp2 temp3
    = temp1 temp2
    = temp0 temp1
    = gInvFirst (send gInv:first())
    (if (> (* (= temp16 Sqrt(temp0)) (while (gInvFirst)(= temp9 NodeValue(gInvFirst)) 
        (if ((send temp9:isKindOf(InvI)) and (send temp9:ownedBy(param1)))
            (send temp9:signal((& (send temp9:signal) $fffb)))
            ++temp0
(            = temp6 CelWide((send temp9:view) (send temp9:loop) (send temp9:cel))
)
            (if (> temp6 temp2)
(                = temp2 temp6
)            )
(            = temp7 CelHigh((send temp9:view) (send temp9:loop) (send temp9:cel))
)
            (if (> temp7 temp1)
(                = temp1 temp7
)            )
        )(else
            (if ((send temp9:isMemberOf(IconI)))
                ++temp3
(                = temp5 (+ temp5 CelWide((send temp9:view) (send temp9:loop) (send temp9:cel)))
)(                = temp7 CelHigh((send temp9:view) (send temp9:loop) (send temp9:cel))
)
                (if (> temp7 temp4)
(                    = temp4 temp7
)                )
            )(else
                (send temp9:signal((| (send temp9:signal) $0004)))
            )
        )
 (= gInvFirst (send gInv:next(gInvFirst)))  )) temp0)
        --temp16
    )
    (if (> temp16 3)
        = temp16 3
    )
    = local5 (/ temp0 temp16)
    (if (< (* temp16 local5) temp0)
        ++local5
    )
    = temp10 proc999_3((+ 4 temp5) (* local5 (+ 4 temp2)))
    = temp11 (* temp16 (+ 4 temp1))
    = temp12 (/ (- 190 temp11) 2)
    = temp13 (/ (- 320 temp10) 2)
    = temp14 (+ temp12 temp11)
    = temp15 (+ temp13 temp10)
    (if ((send gInv:window))
        (send ((send gInv:window)):
            top(temp12)
            left(temp13)
            right(temp15)
            bottom(temp14)
            open()
        )
    )
    = temp20 local5
    (if (temp0)
        = temp18 (+ 2 
            (if ((send ((send gInv:window)):respondsTo(328)))
                (send ((send gInv:window)):yOffset)
            )(else
                0
            )
)
        = temp17 (+ 4 
            (if ((send ((send gInv:window)):respondsTo(327)))
                (send ((send gInv:window)):xOffset)
            )(else
                0
            )
)
        = temp19 temp17
        = gInvFirst (send gInv:first())
        (while (gInvFirst)
            = temp9 NodeValue(gInvFirst)
            (if (not (& (send temp9:signal) $0004) and (send temp9:isKindOf(InvI)))
                (if (not & (send temp9:signal) $0080)
                    = temp6 CelWide((send temp9:view) (send temp9:loop) (send temp9:cel))
                    = temp7 CelHigh((send temp9:view) (send temp9:loop) (send temp9:cel))
                    (send temp9:
                        nsLeft(+ temp17 (/ (- temp2 temp7) 2))
                        nsTop(+ temp18 (/ (- temp1 temp7) 2))
                    )
                    (send temp9:
                        nsRight(+ (send temp9:nsLeft) temp6)
                        nsBottom(+ (send temp9:nsTop) temp7)
                    )
                    (if (--temp20)
                        = temp17 (+ temp17 temp2)
                    )(else
                        = temp20 local5
                        = temp18 (+ temp18 temp1)
                        = temp17 temp19
                    )
                )(else
                    = temp17 (send temp9:nsLeft)
                    = temp18 (send temp9:nsTop)
                )
                (send temp9:show())
                (if (== temp9 param2)
                    (send gGame:setCursor(5 1))
                    = local3 temp9
                    (send local3:highlight())
                    = theGInvFirst gInvFirst
                )
            )
            = gInvFirst (send gInv:next(gInvFirst))
        )
    )
    = temp17 (/ (- (- (send ((send gInv:window)):right()) (send ((send gInv:window)):left)) temp5) 2)
    = gInvFirst (send gInv:first())
    (while (gInvFirst)
        = temp9 NodeValue(gInvFirst)
        (if (not (send temp9:isMemberOf(InvI)))
            = temp6 CelWide((send temp9:view) (send temp9:loop) (send temp9:cel))
            = temp7 CelHigh((send temp9:view) (send temp9:loop) (send temp9:cel))
            = temp11 (- (send ((send gInv:window)):bottom) (send ((send gInv:window)):top))
            (send temp9:
                nsLeft(temp17)
                nsTop(- temp11 temp7)
                nsBottom(temp11)
                nsRight(+ temp17 temp6)
                signal(& (send temp9:signal) $fffb)
                show()
            )
            = temp17 (+ temp17 temp6)
        )
        = gInvFirst (send gInv:next(gInvFirst))
    )
)


(class InvI of IconI
    (properties
        view 0
        loop 0
        cel 0
        nsLeft 0
        nsTop 0
        nsRight 0
        nsBottom 0
        state 0
        cursor 999
        type 16384
        message 3
        modifiers 0
        signal 0
        helpStr 0
        maskView 0
        maskLoop 0
        maskCel 0
        highlightColor 0
        lowlightColor 0
        description 0
        owner 0
        script 0
        value 0
    )

    (method (show)
        (var temp0[4])
        DrawCel(view loop cel nsLeft nsTop -1)
    )


    (method (highlight param1)
        (var temp0, temp1, temp2, temp3, temp4)
        = temp4 
            (if (paramTotal and param1)
                highlightColor
            )(else
                lowlightColor
            )
        = temp0 (- nsTop 2)
        = temp1 (- nsLeft 2)
        = temp2 (+ nsBottom 1)
        = temp3 (+ nsRight 1)
        Graph(4 temp0 temp1 temp0 temp3 temp4 -1 -1)
        Graph(4 temp0 temp3 temp2 temp3 temp4 -1 -1)
        Graph(4 temp2 temp3 temp2 temp1 temp4 -1 -1)
        Graph(4 temp2 temp1 temp0 temp1 temp4 -1 -1)
        Graph(12 - nsTop 2 - nsLeft 2 + nsBottom 2 + nsRight 2 1)
    )


    (method (onMe param1)
        return 
            (if ((super:onMe(param1)))
                not & signal $0004
            )(else
                0
            )
    )


    (method (ownedBy param1)
        return == owner param1
    )


    (method (moveTo theOwner)
        = owner theOwner
        (if (value and (= theOwner gEgo))
            (send gGame:changeScore(value))
            = value 0
        )
        return self
    )


    (method (doVerb param1)
        (switch (param1)
            (case 1
                proc255_4(995 0 description)
            )
        )
    )

)
(class Inv of IconBar
    (properties
        elements 0
        size 0
        height 0
        underBits 0
        oldMouseX 0
        oldMouseY 0
        curIcon 0
        highlightedIcon 0
        prevIcon 0
        curInvIcon 0
        useIconItem 0
        helpIconItem 0
        port 0
        window 0
        state 0
        activateHeight 0
        normalHeading "Roger is carrying:"
        heading 0
        empty "nothing!"
        curScore "Score: %d out of %d"
        showScore 1
        iconBarInvItem 0
        okButton 0
        selectIcon 0
    )

    (method (init)
        = gInv self
        = heading normalHeading
    )


    (method (doit)
        (var theCurIcon, newEvent, newEventType, temp3[4], temp7)
        (while ((= newEvent (Event:new())) and (& state $0020))
            = temp7 0
            (send newEvent:localize())
            (if ((((curIcon and not (send newEvent:modifiers)) and (<> curIcon selectIcon)) and (((== (send newEvent:type) 1) or (((== (send newEvent:type) 4) and (== (send newEvent:message) 13)) and (= temp7 1))) or ((== (send newEvent:type) 256) and (= temp7 1)))) and ((<> curIcon helpIconItem) or (& (send helpIconItem:signal) $0010)))
                (send newEvent:
                    type(16384)
                    message((send curIcon:message))
                )
            )
            MapKeyToDir(newEvent)
            = newEventType (send newEvent:type)
            (if ((== newEventType 1) and (send newEvent:modifiers))
                (self:advanceCurIcon())
                (send newEvent:claimed(1))
            )(else
                (if (((== newEventType 0) and (= theCurIcon (self:firstTrue(186 newEvent)))) and (<> (= theCurIcon (self:firstTrue(186 newEvent))) highlightedIcon))
                    (self:highlight(theCurIcon))
                )(else
                    (if (((== newEventType 1) or ((== newEventType 4) and (== (send newEvent:message) 13))) or (== newEventType 256))
                        (if (IsObject(highlightedIcon) and (self:select(highlightedIcon (== newEventType 1))))
                            (if (== highlightedIcon okButton)
                                break
                            )
                            (if (== highlightedIcon helpIconItem)
                                (if (<> (send highlightedIcon:cursor) -1)
                                    (send gGame:setCursor((send helpIconItem:cursor)))
                                )
                                (if (helpIconItem)
                                    (send helpIconItem:signal((| (send helpIconItem:signal) $0010)))
                                )
                            )(else
                                = curIcon highlightedIcon
                                (send gGame:setCursor((send curIcon:cursor) 1))
                            )
                        )
                    )(else
                        (if (& newEventType $0040)
                            (switch ((send newEvent:message))
                                (case 3
                                    (self:advance())
                                )
                                (case 7
                                    (self:retreat())
                                )
                                (case 1
                                    (self:retreat(local5))
                                )
                                (case 5
                                    (self:advance(local5))
                                )
                                (case 0
                                    (if (& newEventType $0004)
                                        (self:advanceCurIcon())
                                    )
                                )
                            )
                        )(else
                            (if (== newEventType 4)
                                (switch ((send newEvent:message))
                                    (case 9
                                        (self:advance())
                                    )
                                    (case 3840
                                        (self:retreat())
                                    )
                                )
                            )(else
                                (if ((== newEventType 16384) and (= theCurIcon (self:firstTrue(186 newEvent))))
                                    (if (== (send newEvent:message) 5)
                                        (if (theCurIcon and (send theCurIcon:helpStr()))
                                            proc255_4(995 0 (send theCurIcon:helpStr()))
                                        )
                                        (send helpIconItem:signal((& (send helpIconItem:signal) $ffef)))
                                        (send gGame:setCursor(999))
                                    )(else
                                        (if (== theCurIcon okButton)
                                            break
                                        )
                                        (if ((send theCurIcon:isMemberOf(IconI)))
                                            (if ((self:select(theCurIcon not temp7)))
                                                = curIcon theCurIcon
                                                (send gGame:setCursor((send curIcon:cursor) 1))
                                            )
                                        )(else
                                            (if (curIcon)
                                                (if ((send curIcon:isMemberOf(InvI)))
                                                    (send theCurIcon:doVerb((send curIcon:message) (self:indexOf(curIcon))))
                                                )(else
                                                    (send theCurIcon:doVerb((send newEvent:message)))
                                                )
                                            )
                                        )
                                    )
                                )
                            )
                        )
                    )
                )
            )
            (send newEvent:dispose())
        )
        (send newEvent:dispose())
        (self:hide())
    )


    (method (showSelf)
        (var temp0[9])
        (send gSounds:pause())
        (if (gPseudoMouse and (send gPseudoMouse:respondsTo(149)))
            (send gPseudoMouse:stop())
        )
        (if ((send gIconBar:height))
            (send gIconBar:hide())
        )
        (if (not window)
            = window (SysWindow:new())
        )
        (if ((send window:window))
            (send window:dispose())
            = window 0
        )
        (if (not okButton)
            = okButton NodeValue((self:first()))
        )
        = curIcon 0
        (send gGame:setCursor((send selectIcon:cursor)))
        (self:
            show()
            doit()
        )
    )


    (method (show)
        = state (| state $0020)
        localproc_01a2(gEgo (send gIconBar:curIcon))
    )


    (method (hide)
        (if (& state $0020)
            (send gSounds:pause(0))
            = state (& state $ffdf)
        )
        (if (window)
            (send window:dispose())
        )
        (if (IsObject(curIcon) and (send curIcon:isMemberOf(InvI)))
            (send gIconBar:
                curIcon((send ((send gIconBar:useIconItem)):
                        cursor((send curIcon:cursor))
                        yourself()
                    )
)
                curInvIcon(curIcon)
            )
            (if ((send curIcon:cursor))
                (send gGame:setCursor((send curIcon:cursor) 1))
            )
        )
    )


    (method (advance param1)
        (var temp0, temp1, temp2, temp3)
        = temp1 
            (if (paramTotal)
                param1
            )(else
                1
            )
        = temp2 (self:indexOf(highlightedIcon))
        = temp3 (+ temp1 temp2)
        (while (TRUE)
            = temp0 (self:at(
                    (if (<= temp3 size)
                        temp3
                    )(else
                        (% temp3 (- size 1))
                    )
))
            (if (not IsObject(temp0))
                = temp0 NodeValue((self:first()))
            )
            (if (not & (send temp0:signal) $0004)
                break
            )
            ++temp3
        )
        (send gGame:setCursor(global19 1 (+ (send temp0:nsLeft) (/ (- (send temp0:nsRight) (send temp0:nsLeft)) 2)) (- (send temp0:nsBottom) 3)))
        (self:highlight(temp0))
    )


    (method (retreat param1)
        (var temp0, temp1, temp2, temp3)
        = temp1 
            (if (paramTotal)
                param1
            )(else
                1
            )
        = temp2 (self:indexOf(highlightedIcon))
        = temp3 (- temp2 temp1)
        (while (TRUE)
            = temp0 (self:at(temp3))
            (if (not IsObject(temp0))
                = temp0 NodeValue((self:last()))
            )
            (if (not & (send temp0:signal) $0004)
                break
            )
            --temp3
        )
        (send gGame:setCursor(global19 1 (+ (send temp0:nsLeft) (/ (- (send temp0:nsRight) (send temp0:nsLeft)) 2)) (- (send temp0:nsBottom) 3)))
        (self:highlight(temp0))
    )


    (method (advanceCurIcon)
        (var theCurIcon)
        = theCurIcon curIcon
        (while ((send (= theCurIcon (self:at((% (+ (self:indexOf(theCurIcon)) 1) size)))):isMemberOf(InvI)))
        )
        = curIcon theCurIcon
        (send gGame:setCursor((send curIcon:cursor) 1))
    )


    (method (ownedBy param1)
        (self:firstTrue(202 param1))
    )

)
