All pastes #2082678 Raw Edit

Stuff

public text v1 · immutable
#2082678 ·published 2011-09-25 08:01 UTC
rendered paste body
Array goto_dirs-->MAX_GOTO_STEPS;
Array goto_rooms-->MAX_GOTO_STEPS;

Global current_dir_index = 0;
Global current_room_index = 0;

[ PushInfo dir room;
    goto_dirs-->current_dir_index++ = dir;
    goto_rooms-->current_room_index++ = room;
];

[ CurRoom;
    return goto_rooms-->(current_room_index-1);
];

[ PopInfo;
    current_room_index--;
    current_dir_index--;
];

[ MarkDone room;
    if (room == nothing)
        return;

    room.goto_checked = true;
];

[ IsDone room;
    if (room == nothing)
        return;

    if (room.goto_checked)
        rtrue;

    rfalse;
];

[ CheckPath room i;
    for (i = 0: i <= current_room_index: i++)
        if (goto_rooms-->i == room)
            rtrue;
    rfalse;
];

[ route start stop;
    if (start == stop)
        "You're already there!";
    if (start == nothing || stop == nothing)
        "You can't find your way to nowhere!";

    print "pathing ", (name) start, " to ", (name) stop, "^";
    current_room_index = 1;
    current_dir_index = 0;
    goto_rooms-->0 = start;
    return _route(start, stop);
];

[ ResolveDir dir cur;
    if (dir provides door_dir)
    {
        dir = dir.door_dir;
    }
    if (~~(cur provides dir))
        rfalse;
    dir = cur.dir;
    if (dir provides resolve_door)
    {
        dir = dir.resolve_door(cur);
    }
    if (metaclass(dir) == routine)
    {
        dir = dir();
    }
    if (~~(dir ofclass Room))
    {
        rfalse;
    }

    return dir;
];

Global route_steps = 500;

[ _route start stop cur dir room;
    route_steps--;
    if (route_steps < 0)
        rfalse;

    MarkDone(start);

    cur = CurRoom();

    if (CheckPath(stop))
        rtrue;

    if (cur == nothing)
        rfalse;

    objectloop (dir in compass)
    {
        room = ResolveDir(dir, cur);
        if (~~room || room == nothing)
            continue;

        if (IsDone(room))
            continue;

        if (room == stop)
        {
            PushInfo(dir, room);
            rtrue;
        }
    }

    objectloop (dir in compass)
    {
        room = ResolveDir(dir, cur);

        if (~~room || room == nothing)
            continue;

        if (IsDone(room))
            continue;

        PushInfo(dir, room);
        MarkDone(room);

        if (room == stop)
        {
            rtrue;
        }
        else
        {
            _route(start, stop);
            if (CheckPath(stop))
                rtrue;
        }

        PopInfo();
    }

    if (CheckPath(stop))
        rtrue;

    rfalse;
];

[ RouteAndPrint start stop i;
    if (~~route(start, stop))
        print "Sorry, can't find my way there.";

    print "From ";

    for (i = 0: i < (current_room_index-1): i++)
        print (name) goto_rooms-->i, " go ", (name) goto_dirs-->i, " to^";

    print (name) noun, "!";
];