# pubcore.py 6/01/98 JJS # # This module defines datatypes and constants used by most other # PUB modules. You shouldn't mess with this file unless you # really know what you're doing. # # Use this module with: # from pubcore import * # #---------------------------------------------------------------------- # import standard Python modules needed by the functions below import string import types import copy import whrandom import regex # import the PUB module which declares "global" variables import pub # declare some constants the = 'the' a = 'a' The = 'The' A = 'A' OK = 1 CANCEL = 0 BEGIN = 1 FINISH = 2 RUNNING = 1 QUIT = 0 TRUE = 1 FALSE = 0 # function to capitalize a string def cap(s): if not s: return s return string.upper(s[0]) + s[1:] # function to determine whether it's a string def isString(x): return type(x) == types.StringType # function to determine whether it's an Instance def isInstance(x): return type(x) == types.InstanceType # function to determine whether it's a number, or interpretable as one def isInt(x): try: string.atoi(x) return TRUE except: return type(x) == types.IntType # function to force it to be a number def toInt(x): if type(x) == types.IntType: return x try: return string.atoi(x) except: return 0 # function to replace all occurances of one substring with another def replace(what,withwhat,inwhat): # we'll find the fastest way to do this later... return string.join(string.split(inwhat,what),withwhat) # function to strip commas and periods from a string def stripPunctuation(x): out = string.join(string.split(x,','),'') out = string.join(string.split(out,'.'),'') out = string.join(string.split(out,'!'),'') out = string.join(string.split(out,'?'),'') return out # function to save the game def savegame(filename='pub.dat'): import sys import pub import pubverbs import picklemod pub.lastroom = None # (to prevent auto-placement of objects) f = open(filename, 'w') picklemod.save( f, pubverbs, pub, sys.modules['__main__'] ) f.close() # function to restore the game def restoregame(filename='pub.dat'): import sys import pub import pubverbs import picklemod pub.lastroom = None # (to prevent auto-placement of objects) f = open(filename, 'r') picklemod.restore( f, pubverbs, pub, sys.modules['__main__'] ) f.close() #---------------------------------------------------------------------- # event -- a class which keeps something to be executed in the future # class Event: # initialization method def __init__(self,pObject=None,pCode=None,pCmd=None): if not pObject: return # must be unpickling self.object = pObject self.cmd = copy.copy(pCmd) self.code = pCode # print "Event created: " + str(self) def __str__(self): return '< Event: ' + str(self.object) + ',' + str(self.code) + ' >' def Perform(self): # print 'Performing: ' + str(self) object = self.object cmd = self.cmd exec self.code def RefersTo(self,pWhom): if self.object == pWhom or \ self.cmd and self.cmd.actor == pWhom: return TRUE return FALSE #---------------------------------------------------------------------- # scheduler -- keeps track of the world clock, calls events, etc. # class Scheduler: # initialization method def __init__(self,pTimeString='12:00'): timeparts = string.split(pTimeString,':') self.minutes = string.atoi(timeparts[0])*60 \ + string.atoi(timeparts[1]) self.events = {} def __str__(self): return '< Scheduler at time ' + self.GetTime() \ + ' and ' + str(len(self.events)) + ' events >' def GetTime(self): day = self.minutes / 1440 hour = (self.minutes%1440) / 60 minute = self.minutes%1440 % 60 if day: return "%d:%02d (Day %s)" % (hour,minute,day) return "%d:%02d" % (hour,minute) def AddAbsEvent(self,pAbsTime,pEvent): if pAbsTime < self.minutes: print "WARNING: scheduling event for a past time" self.minutes = pAbsTime if self.events.has_key(pAbsTime): self.events[pAbsTime].append(pEvent) else: self.events[pAbsTime] = [pEvent] def AddEvent(self,pRelTime,pEvent): time = self.minutes + pRelTime if self.events.has_key(time): self.events[time].append(pEvent) else: self.events[time] = [pEvent] def Update(self): if not self.events: return eventkeys = self.events.keys() # get event times eventkeys.sort() # sort them nexteventkey = eventkeys[0] # find earliest time self.minutes = nexteventkey # update clock eventlist = self.events[nexteventkey] del self.events[nexteventkey] # remove from the queue for e in eventlist: # print '[' + self.GetTime() + '] ', e.Perform() # perform scheduled events def HasEventFor(self, pFor): if not self.events: return FALSE for eventlist in self.events.values(): if filter(lambda x,a=pFor: x.RefersTo(a), eventlist): return TRUE return FALSE #---------------------------------------------------------------------- # Command -- stores references for variarious parts of a command # class Command: def __init__(self): self.Clear() def Clear(self): self.actor = '' self.verb = '' self.dirobj = '' self.toobj = '' self.inobj = '' self.atobj = '' self.withobj = '' def __str__(self): out = '<<' if (self.actor): out = ' Actor: ' + str(self.actor) if (self.verb): out = out + ' Verb: ' + str(self.verb) if (self.dirobj): out = out + ' Obj: ' + str(self.dirobj) if (self.toobj): out = out + ' To: ' + str(self.toobj) if (self.inobj): out = out + ' In: ' + str(self.inobj) if (self.atobj): out = out + ' At: ' + str(self.atobj) if (self.withobj): out = out + ' With: ' + str(self.withobj) out = out + ' >>' return out def StuffString(self,pStr,pFor=None): if string.find(pStr,'<') < 0 or string.find(pStr,'>') < 0: return pStr str = replace('',self.actor(0,pFor),pStr) str = replace('',cap(self.actor(0,pFor)),str) str = replace('',self.actor(The,pFor),str) str = replace('',self.actor(the,pFor),str) str = replace('',self.actor(A,pFor),str) str = replace('',self.actor(a,pFor),str) if isInstance(self.dirobj): str = replace('',self.dirobj(0,pFor),str) str = replace('',cap(self.dirobj(0,pFor)),str) str = replace('',self.dirobj(The,pFor),str) str = replace('',self.dirobj(the,pFor),str) str = replace('',self.dirobj(A,pFor),str) str = replace('',self.dirobj(a,pFor),str) # else: print "dirobj not an instance" if isInstance(self.inobj): str = replace('',self.inobj(0,pFor),str) str = replace('',cap(self.inobj(0,pFor)),str) str = replace('',self.inobj(The,pFor),str) str = replace('',self.inobj(the,pFor),str) str = replace('',self.inobj(A,pFor),str) str = replace('',self.inobj(a,pFor),str) if isInstance(self.toobj): str = replace('',self.toobj(0,pFor),str) str = replace('',cap(self.toobj(0,pFor)),str) str = replace('',self.toobj(The,pFor),str) str = replace('',self.toobj(the,pFor),str) str = replace('',self.toobj(A,pFor),str) str = replace('',self.toobj(a,pFor),str) str = replace('