BODY_TYPES = {"human":["head","arms","body","legs","feet","left hand","right hand"], "humanoid":["head","arms","body","legs","feet","left hand","right hand","tail"], "taur":["head","arms","body","legs","feet","left hand","right hand","tail","taurbody"], } ROOM_DESC_DEFAULT = ("A snowy plain.","A snowy plain under a blue sky.") class Person: def __init__(self,**options): self.name = options.get("name") self.full_name = options.get("full_name") self.world = options.get("world") self.room = options.get("room") self.inventory = [] self.stats = options.get("stats") self.body_type = options.get("body_type") equipment_slots = BODY_TYPES.get(self.body_type) self.equipment = dict.fromkeys(equipment_slots,None) def GetStat(self,stat): return self.stats.get(stat) def Equip(self,what,where): slot_exists = self.equipment.has_key(where) if not slot_exists: return False self.equipment[where] = what what.BeHeldBy(self) return True def Unequip(self,where): slot_exists = self.equipment.has_key(where) if not slot_exists: return False item = self.equipment[where] self.equipment[where] = None item.BeHeldBy(None) return item class Item: def __init__(self,**options): self.name = options.get("name") self.category = options.get("category") self.desc = options.get("desc","A "+self.category+".") self.description = options.get("description","A "+self.category+" of some sort.") self.holder = None def BeHeldBy(self,holder): self.holder = holder class Exit: def __init__(self,**options): self.name = options.get("name","Dennis") self.destination = options.get("destination") class Place: def __init__(self,**options): self.ID = options.get("ID") self.world = options.get("world") self.game = options.get("game") self.name = options.get("name","Empty Ground") self.desc = options.get("desc",ROOM_DESC_DEFAULT[0]) self.description = options.get("description",ROOM_DESC_DEFAULT[1]) self.contents = [] self.people = [] self.exits = {} def AddExit(self,**options): new_exit = Exit(**options) self.exits[new_exit.name] = new_exit def AddPerson(self,person,hidden=False): self.people.append(person) def AddThing(self,thing,hidden=False): self.contents.append(thing) ## if not hidden: ## self.Announce({"headline":"arrival","who":entity}) def RemovePerson(self,person,hidden=False): self.people.remove(person) def RemoveThing(self,thing,hidden=False): self.contents.remove(thing) ## if not hidden: ## self.Announce({"headline":"departure","who":entity}) ## def Announce(self,message): ## for e in self.entities: ## e.GetMessage(message) class World: def __init__(self,**options): self.name = options.get("name","The World") self.places = {} self.entities = [] def AddPlace(self,**options): ## Assign a unique place ID. ID = 0 while ID in self.places: ID += 1 options["ID"] = ID place = Place(**options) self.places[ place.ID ] = place return place def AddNewEntity(self,**options): options["world"] = self if options.get("person"): entity = Person(**options) else: entity = Entity(**options) ## Where should the person/entity start? Find by ID or name. place = options.get("place") if type(place) is str: place = self.FindPlaceByName(place) if not place: raise "Error: Trying to put an entity in a nonexistent place." place.AddEntity(entity) def FindPlaceByName(self,name): """Return one place with this exact name, if any. If multiple results are found, there's no guarantee which is returned.""" results = [place for place in self.places.values() if place.name == name] if len(results) == 1: return results[0] elif not results: return "No such place." else: return results[0] def ListPlaces(self): text = "" for key in self.places: place = self.places[key] text += "\n"+place.name+" ("+(place.desc or "No description")[:40]+")\n" exits = [DIRECTIONS[n] for n in range(6) if place.exits[n] and self.places.has_key(place.surrounding_coords[n]) ] text += "Exits: "+FancyList(exits)+"\n" entities = place.entities if entities: text += "Entities here:\n" for e in entities: text += " -"+e.name+"\n" print text def MoveEntity(self,entity,direction,sanity_check=True): """Move an entity if possible.""" print "Trying to move entity "+entity.name+" in direction "+direction current_coords = entity.coords current_room = self.places.get(current_coords) vectors = {"north":(0,-1,0),"east":(1,0,0), "south":(0,1,0),"west":(-1,0,0), "up":(0,0,1),"down":(0,0,-1)} vector = vectors.get(direction) if not vector: return False ## You can't go that way. target_coords = (current_coords[0]+vector[0], current_coords[1]+vector[1], current_coords[2]+vector[2]) target_room = self.places.get(target_coords) print "Target coords: "+str(target_coords)+". Room: "+str(target_room) if target_room: ## exit_allowed = target_room.exits[ """some shortcut, NYI""" ] ## if not exit_allowed: ## return False ## You can't go that way. current_room.RemoveEntity(entity) target_room.AddEntity(entity) return True ## Test ## Make a person. person = Person(name="Ossy", full_name="Ossy the Roo", body_type="humanoid", ) boomerang = Item(name="Boomerang", category="weapon") person.Equip(boomerang,"left hand") assert person.equipment["left hand"] is boomerang assert boomerang.holder is person ## Make a place. library = Place(name="Library of Wikipedia: Stacks AAAAB-AAAAC", description="A huge space full of shelves.", desc="Part of a huge library.", ) mcr = Place(name="Master Control Room", description="A room full of awesome computers and stuff.", desc="It's just awesome.", ) library.AddExit(name="North",destination=mcr) assert library.exits["North"].destination is mcr boomerang = person.Unequip("left hand") mcr.AddThing(boomerang) assert boomerang in mcr.contents