
Bob Meets NUON
by David Betz

Listing One
///////////
// Hazzard
Hazzard = new Object();
define Hazzard.initialize(cave)
{
    this.location = cave;
    cave.AddHazzard(this);
    return this;
}
define Hazzard.Move(cave)
{
    if (this.location != cave) {
        this.location.RemoveHazzard(this);
        this.location = cave;
        cave.AddHazzard(this);
    }
    return this;
}
///////////
// Pit
Pit = new Hazzard;
define Pit.Bump(game)
{
    game.Over("YYYIIIIEEEE . . . Fell in pit!");
}

Listing Two
#include "bob.h"
static int CloseConsoleStream(BobStream *s)
{
    return 0;
}
static int ConsoleStreamGetC(BobStream *s)
{
    return getchar();
}
static int ConsoleStreamPutC(int ch,BobStream *s)
{ 
    return putchar(ch);
}
BobStreamDispatch consoleDispatch = { 
     CloseConsoleStream, ConsoleStreamGetC, ConsoleStreamPutC
};
typedef struct {
    BobStreamDispatch *d;
} ConsoleStream;
ConsoleStream consoleStream = { &consoleDispatch };
BobInterpreter *InitBob(void)
{
    BobInterpreter *c;
    /* make the interpreter context */
    if (!(c = BobMakeInterpreter(HEAP_SIZE,0,STACK_SIZE)))
        return NULL;
    /* use the standard library */
    BobEnterLibrarySymbols(c);
    /* allow access to the compiler */
    BobUseEval(c);
    /* establish an error handler */
    c->errorHandler = ErrorHandler;
    /* protect bob values */
    c->protectHandler = ProtectHandler;
    /* setup standard i/o */
    c->standardInput = (BobStream *)&consoleStream;
    c->standardOutput = (BobStream *)&consoleStream;
    c->standardError = (BobStream *)&consoleStream;
    /* return the interpreter context */
    return c;
}

Listing Three
/* built-in methods */
static BobValue BIF_HideWidget(BobInterpreter *c);

/* built-in properties */
static BobValue BIF_WidgetX(BobInterpreter *c,BobValue obj);
static void BIF_SetWidgetX( BobInterpreter *c, BobValue obj, BobValue value);
/* 'Widget' methods */
static BobCMethod widgetMethods[] = {
BobMethodEntry( "Hide", BIF_HideWidget                 ),
/* more methods */
BobMethodEntry( 0,      0                              )
};
/* 'Widget' properties */
static BobVPMethod widgetProperties[] = {
BobVPMethodEntry(   "x",  BIF_WidgetX,    BIF_SetWidgetX ),
/* more properties */
BobVPMethodEntry(   0,    0,              0              )
};
/* NuiInitBobWidgets - initialize the 'Widget' object */
int NuiInitBobWidgets(BobInterpreter *c)
{
    /* make the 'Widget' type */
    if (!(nuiBobWidgetType = BobEnterCPtrObjectType( c, nuiEventTargetType,
                                "Widget", widgetMethods, widgetProperties)))
        return FALSE;
    /* return successfully */
    return TRUE;
}

Listing Four
/* BIF_HideWidget - built-in method 'Hide()' */
static BobValue BIF_HideWidget(BobInterpreter *c)
{
    int hideWidgetBackgroundP = -1;
    NuiWidget *widget;
    BobParseArguments(
        c, "P=*|B", &widget, nuiBobWidgetType, &hideWidgetBackgroundP);
    if (!widget) {
        TRACE("Operation attempted on inactive widget\n");
        return c->falseValue;
    }
    NuiHideWidget(widget);
    switch (hideWidgetBackgroundP) {
    case TRUE:
        NuiHideWidgetBackground(widget);
        break;
    case FALSE:
        NuiShowWidgetBackground(widget);
        break;
    }
    return c->trueValue;
}

Listing Five
/* BIF_WidgetX - built-in property 'x' */
static BobValue BIF_WidgetX( BobInterpreter *c, BobValue obj)
{
    NuiWidget *widget = (NuiWidget *)BobCObjectValue(obj);
    if (!widget) {
        TRACE("Operation attempted on inactive widget\n");
        return c->nilValue;
    }
    return BobMakeInteger(c,PageFileSwap16(widget->pageFileWidget->x));
}

/* BIF_SetWidgetX - built-in property 'x' */
static void BIF_SetWidgetX( BobInterpreter *c, BobValue obj, BobValue value)
{
    NuiWidget *widget = (NuiWidget *)BobCObjectValue(obj);
    if (widget) {
        if (BobIntegerP(value))
            widget->window.rect.x = BobIntegerValue(value);
        else
            ConsolePrintF("Expecting integer value\n");
    }
    else
        TRACE("Operation attempted on inactive widget\n");
}

Listing Six
<nml>
<textStyle name='welcome'
           font='fonts/system.t2k'
           color='black'
           size='30'/>
<textStyle name='prompt'
           font='fonts/system.t2k'
           color='goldenrod'
           size='24'/>
<script src='scripts/wumpus.bbo'/>
<body width='720' height='480' bgColor='peachPuff' start='start'>
<group x='20' y='10'>
<widget type='text'
        textStyle='welcome' 
        x='80'
        y='40'
        width='250'
        height='30'
        align='left'
        value='Welcome to Hunt the Wumpus!'/>
<widget name='pic'
        type='image'
        src='images/wumpus.gif'
        x='80'
        y='100'/>
<widget name='start'
        type='text'
        highlightTextStyle='prompt' 
        x='80'
        y='300'
        width='250'
        height='24'
        align='left'
        onClick='topFrame.LoadPage("pages/wumpus.npg")'
        value='Press Enter to Start the Game...'/>
</group>
</body>
</nml>





4

