Modeling the World Via Text Adventures

Ovid on 2005-10-29T18:52:09

I've been ranting a bit about real-world modeling and databases. The real-world is not relational so I want to be able to create a hierarchical model in a database. Unfortunately, autarch had the cheek to point out that the real world is not hierarchical either.

Damn it, he's right. So I decided that in order for me to get a better handle on the problem space I need to create the model of a world. So far it's pretty simplistic but I can put items in other items and take them out again.

sub put {
    my ( $self, $thing ) = @_;
    if ( $self->contains($thing) ) {
        die "\u$thing is already inside of $self.\n";
    }
    elsif ( !$self->capacity ) {
        die "You can't put anything in $self.\n";
    }
    elsif ( $self->curr_capacity < $thing->size ) {
        die "There's not enough room to fit $thing inside of $self.\n";
    }
    else {
        $thing->container($self);
        $self->{gwt_contains}{$thing} = $thing;
        return $self;
    }
}

sub take {
    my ($self, $thing) = @_;
    if ( ! $self->contains($thing) ) {
        die "\u$thing is not inside of $self.\n";
    }
    return delete $self->{gwt_contains}{$thing};
}

In order to make this work, I have a World object and the world is populated by Things. A thing is abstract and corresponds to a class. For a given thing class, each concrete thing must have a unique set of adjectives which describe it.

my $world = Games::World->new;
$world->add({
    name  => 'box',
    class => 'Games::World::Thing::Box'
});

my $box = Games::World::Thing::Box->new({
    adjectives => 'red',
    size       => 15,
    capacity   => 12,
});

my $large_box = Games::World::Thing::Box->new({
    adjectives => ['red', 'large'],
    size       => 25,
    capacity   => 20,
});

I can now put the red box inside of the large red box but I can't put the large red box inside of the red box. Then things start to get difficult. What if I create an item with a capacity to hold the large box but the size to fix in the smaller box? Can I stick the large box in the new thing and then shove it into the smaller box? What happens? You probably get Zork.

adjectives is also a problem. In reality, I should probably list distinguishing properties such as size, color, weight, location, etc. Then the world should simply figure out how to describe and uniquely identify an item. Descriptions should be based on visible characteristics such as color, size and location. However, if someone tries to lift something which is too heavy, even if it's a small red box, then they find out more characteristics. Modeling a world is very difficult and I need to be able to ensure that I do not create two things which cannot be differentiated.

I should add that part of the reason I've put the Higher Order Perl parser on the CPAN is my goal of slapping a natural language interface over all of this. I really hadn't set out to make a text adventure (this time), but it really does give me insight into the problems I'm trying to solve.

Note: this is all pretty similar to Games::3D but the code Tels wrote is specifically for 3D rendering in game engines. It does a lot of the things I want, but doesn't do others.


Inform

jdavidb on 2005-10-30T01:00:19

Why don't you try some programming in Inform? It's a little different paradigm than most programming languages I've seen, and might help you think about things differently?

Re:Inform

Ovid on 2005-10-30T23:29:54

I've thought about that. Rael was telling me about his experiments with Inform and he was thinking about using that or something similar to make a natural language interface for some of the stuff he was doing. If I recall correctly, it was basically something which (for example), if someone sent you an invitation for a meeting, you could type "accept the invitation" and your calendar would automatically be updated and a message would be sent back saying you had accepted.

As for why I didn't go that route, I really wanted to play around more with the "Higher Order Perl" parser. I've written a large grammar with it already and I wanted to be able to do something in pure Perl which allowed me to play to my strengths and get something up and running quickly.