# Quest, a computer role playing game system # Copyright (C) 2002 Willem Robert van Hage, details in the COPYING file. package player; use tree; use misc; use Data::Dumper; $Data::Dumper::Indent = 1; sub new { my $self = {}; $self->{world} = {}; $self->{location} = undef; $self->{flags} = {}; $self->{stats} = { strength => 12 }; $self->{inventory} = { bag => { name => 'bag', description => 'A pretty bag', picture => 'bag.gif', contents => { key => { name => 'key', description => 'A golden key' } }}}; return $self } sub room:lvalue { my($self,$name)=@_; $self->{world}->{$name} } sub item:lvalue { my($self,$name)=@_; $self->{inventory}->{$name} } sub flag:lvalue { my($self,$name)=@_; $self->{flags}->{$name} } sub door:lvalue { my($self,$name)=@_; $self->{location}->{door}->{$name} } sub object:lvalue { my($self,$name)=@_; $self->{location}->{object}->{$name} } sub default_action { my($self,$obj)=@_; (keys %{$obj->{action}})[0] } sub take { my($self,$name)=@_; move($self,$name,"location/object","inventory") } sub drop { my($self,$name)=@_; move($self,$name,"inventory","location/object") } sub move { my ($self,$name,$from,$to) = @_; my $from_where = tree::query($self, $from); my $to_where = tree::query($self, $to); $to_where->{$name} = $from_where->{$name}; delete $from_where->{$name}; return $to_where->{$name} } sub move_to { my ($self,$room) = @_; return $self->{location} = $self->{world}->{$room->{name}} } sub remember { my ($self,$data) = @_; remember_room($self,$data->{room}->{$_}) foreach (keys %{$data->{room}}); return $self } sub remember_room { my ($self,$room) = @_; my $name = $room->{name}; unless (room($self,$name)) { room($self,$name) = $room } elsif (room($self,$name)->{version} < $room->{version}) { room($self,$name) = $room } return room($self,$name) } sub forget_room { my ($self,$name) = @_; my $room = room($self,$name); delete $self->{world}->{$name}; return $room } 1;