Completed room loader. Major fixes to room. Added test adventure for rooms.

* pyoo/interpret.py: Interpreter: Added a place cache for looking up places by name.
                             remove try around handle_enter and handle_move.

* pyoo/placeloader.py: New class. Loads a simple textual fomat for a set of potentially connetcted places.

* pyoo/things.py: Added go <direction> verb to Place.
             Separated go mechanic to do_go.

* roomtest.txt: A small set of connected places to test PlaceLoader.
* testrooms.py: A small adventure that loads roomtest.txt, demonstrating
                PlaceLoader and subclassing Place.
main
Cas Rusnov 9 years ago
parent 8bb60b4203
commit 5fdb1512a4
  1. 1
      pyoo/__init__.py
  2. 20
      pyoo/interpret.py
  3. 46
      pyoo/placeloader.py
  4. 13
      pyoo/things.py
  5. 40
      roomtest.txt
  6. 42
      testrooms.py

@ -3,3 +3,4 @@
from .interpret import Interpreter, PyooError, PyooVerbNotFound
from .base import verb, prepositions, normalized_preps
from .things import Thing, Container, Place, Player
from .placeloader import PlaceLoader

@ -55,6 +55,7 @@ class Interpreter(object):
self.update_namecache()
self.update_commandcache()
self.update_placecache()
def update_namecache(self):
self.namecache = list()
@ -62,6 +63,12 @@ class Interpreter(object):
for name in obj.names:
self.namecache.append((name, obj))
def update_placecache(self):
self.placecache = list()
for obj in self.rooms:
for name in obj.names:
self.placecache.append((name, obj))
def update_commandcache(self):
self.commandcache = list()
for obj in self.contents:
@ -80,18 +87,17 @@ class Interpreter(object):
def get_namematches(self, name):
return [x for x in self.namecache if fnmatch.fnmatch(name, x[0])]
def get_placematches(self, name):
return [x for x in self.placecache if fnmatch.fnmatch(name, x[0])]
def handle_move(self, newroom):
self.room = newroom
if self.player:
try:
self.player.handle_move(newroom)
except: pass
self.player.handle_move(newroom)
try:
self.room.handle_enter(player)
except:
pass
self.room.handle_enter(self.player)
self.update()
def handle_get(self, thing):
pass

@ -0,0 +1,46 @@
from .things import Place
(ST_START, ST_DESC, ST_EXITS) = range(3)
class PlaceLoader(object):
def __init__(self, infile, baseplace=Place):
"""Create the factory by reading the contents of infile (an iteratable object [file-like or otherwise]). Specify the base class for place with baseplace."""
self.base = baseplace
self.places = dict() #indexed by name
if infile:
self.process_file(infile)
def process_file(self, inf):
state = ST_START
rm = None
desc = list()
for line in inf:
line = line.strip()
if state == ST_START:
rm = self.base(line)
rm.temp_exits = list()
state = ST_DESC
desc = list()
elif state == ST_DESC:
if line == '.':
state = ST_EXITS
rm.description = desc
else:
desc.append(line)
elif state == ST_EXITS:
if line == '.':
state = ST_START
self.places[rm.name] = rm
rm = None
else:
rm.temp_exits.append(line)
# assemble the exits
for place in self.places.values():
for ext in place.temp_exits:
names, destination = ext.split(' ',1)
for nm in names.split(','):
place.ways[nm] = self.places[destination]
place.update_go()

@ -73,14 +73,21 @@ class Place(Container):
# this verb expects to be annotated from update_go. We never want it ot be at the top of a match list by its deault name either
@verb('#go','none','none','none')
def go(self, verbname, dobjstr, prepstr, iobjstr, dobj, iobj, argstr):
self.do_go(verbname)
@verb('go,move,walk,run', 'any','none','none')
def go_dir(self, verbname, dobjstr, prepstr, iobjstr, dobj, iobj, argstr):
self.do_go(dobjstr)
def do_go(self, direction):
if self.interpreter:
if self.ways.has_key(verbname):
self.interpreter.handle_move(self.ways[verbname])
if self.ways.has_key(direction):
self.interpreter.handle_move(self.ways[direction])
def update_go(self):
# note does no actually remove items from the go verb in case the descender is overloading.
# also note, the interpreter needs to have update() called after this is called.
for direction in ways:
for direction in self.ways:
if not direction in self.go.names:
self.go.names.append(direction)

@ -0,0 +1,40 @@
Porch
This is a small, fairly nondescript porch covered on two sides with a neck-high fence. It is made of old boards nailed down instead of screwed.
To the north is the front door, slightly ajar.
.
n,north Play Room
.
Play Room
This is a medium-sized room with light wood parcade floor. There are toys of every description strewn about willy-nilly, two large baskets of stuffed animals and various tables adorned with books and drawing supplies. Large south-facing windows stream bright winter sunlight, warming the room.
To the south is the front door. To the east the room empties into the living room, to the north it continues into the hallway.
.
s,south Porch
n,north Hallway
e,east Living Room
.
Hallway
This is a narrow wood-floored hallway, echoing with every sound.
Several doors connect to the hallway, all closed. It also contiues to the south into the play room, and east into the kitchen.
.
s,south Play Room
e,east Kitchen
.
Kitchen
This is a small linolium-floored kitchen, with red counters, a double stainless steel sink filled with dishes and a noisy refridgerator. It is illuminated from above by recessed flourescent lights, although only one bulb seems to be working.
The room contiues to the south into the living room, and to the west into the hallway.
.
s,south Living Room
w,west Hallway
.
Living Room
This small intimate livingroom with light wood parkade flooring is dominated by a large wampa rug on the floor and a TV opposite a three seat red couch. A window to the south has the shades draw for enhanced viewing.
The room continues to the west past the couch into the play room, and to the north into the kitchen.
.
w,west Play Room
n,north Kitchen
.

@ -0,0 +1,42 @@
import pyoo
from pyoo import PlaceLoader, Thing, verb, Interpreter, Container, Place, Player, PyooVerbNotFound
class DescriptivePlace(Place):
def handle_enter(self, player):
self.do_look()
@verb('look,l', 'none','none','none')
def look(self, verbname, dobjstr, prepstr, iobjstr, dobj, iobj, argstr):
self.do_look()
def do_look(self):
print self.name
if isinstance(self.description, basestring):
print self.description
else:
for line in self.description:
print line
loader = PlaceLoader(file('roomtest.txt','r'), DescriptivePlace)
game = Interpreter([], Player(), loader.places.values(), None)
porch = game.get_placematches('Porch')[0][1]
run = True
# REPL
if __name__ == '__main__':
game.handle_move(porch)
while (run):
cmd = ''
try:
cmd = raw_input('>')
except EOFError:
run = False
if cmd.startswith('quit'):
run = False
else:
try:
game.interpret(cmd)
except PyooVerbNotFound:
print "I don't understand that."
print "Bye!"
Loading…
Cancel
Save