summaryrefslogtreecommitdiff
path: root/lib/gamecmd.py
blob: 4516a7c4a47ed7ae34a72ab8d1b29ca8e652f845 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# Murder in the Park - A Robotic Mystery
# Copyright (C) 2008 Jeffrey Armstrong (a.k.a. PrintStar)
# http://jeff.rainbow-100.com/
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 
# A full version of the license terms is available in LICENSE.

import cmd
from gamedb import *
from prep import *
from unsure import *
from custom import *

PROMPT = '>> '

class GameCmd(cmd.Cmd):
    def __init__(self,db=None):
        cmd.Cmd.__init__(self)
        
        if db:
            self.db = db
        else:
            try:
                self.db = GameDB()
            except IOError:
                print 'Error loading default game file...'
    
        self.prompt = PROMPT
    
    def do_n(self,args):
        """Moves north..."""
        success = self.db.moveNorth()
        self.ackMove(success)
        
    def do_s(self,args):
        """Moves south..."""
        success = self.db.moveSouth()
        self.ackMove(success)
    
    def do_e(self,args):
        """Moves east..."""
        success = self.db.moveEast()
        self.ackMove(success)
    
    def do_w(self,args):
        """Moves west..."""
        success = self.db.moveWest()
        self.ackMove(success)
        
    def do_u(self,args):
        """Moves up..."""
        success = self.db.moveUp()
        self.ackMove(success)
    
    def do_d(self,args):
        """Moves down..."""
        success = self.db.moveDown()
        self.ackMove(success)
    
    def do_north(self,args):
        """Moves north..."""
        success = self.db.moveNorth()
        self.ackMove(success)
        
    def do_south(self,args):
        """Moves south..."""
        success = self.db.moveSouth()
        self.ackMove(success)
    
    def do_east(self,args):
        """Moves east..."""
        success = self.db.moveEast()
        self.ackMove(success)
    
    def do_west(self,args):
        """Moves west..."""
        success = self.db.moveWest()
        self.ackMove(success)
        
    def do_up(self,args):
        """Moves up..."""
        success = self.db.moveUp()
        self.ackMove(success)
    
    def do_down(self,args):
        """Moves down..."""
        success = self.db.moveDown()
        self.ackMove(success)
    
    def do_look(self,args):
        """Looks at an object or just around in general"""
        if len(args.strip()) == 0:
            self.outputRoom()
            return
        
        cleaner = removePrep(args)
        
        if isDirection(cleaner.strip()):
            self.outputLook(cleaner.strip())
        else:
            #print cleaner.strip()
            self.outputLookAt(self.db.lookAt(cleaner.strip()))
    
    def do_examine(self,args):
        if len(args.strip())==0:
            print "Examine what?"
        
        self.do_look('at '+args.strip())
    
    def do_i(self,args):
        """List inventory..."""
        inv = self.db.getInventory()
        if len(inv) == 0:
            'You currently have no items in your inventory'
            return
        print 'Current Inventory:'
        for x in inv:
            print '  '+x
        
    def do_inv(self,args):
        """List inventory..."""
        self.do_i(args)
        
    def do_inventory(self,args):
        """List inventory..."""
        self.do_i(args)
        
    def do_get(self,args):
        """Picks up an item in the room..."""
        success = self.db.pickUp(args)
        if not success:
            print "I don't see any %s to pick up" % args
        elif success == True:
            print 'Picked up %s' % args
        else:
            print success
            
    def do_drop(self,args):
        """Drops an inventory item"""
        success = self.db.drop(args)
        if not success:
            print "You're not carrying %s" % args
        elif success == True:
            print "Dropped %s" % args
    
    def do_exit(self,args):
        """Quits the game..."""
        return self.do_quit(args)
        
    def do_quit(self,args):
        """Quits the game..."""
        self.db.end()
        return -1
    
    def do_save(self,args):
        """Saves the game to a specified file"""
        if len(args.strip()) == 0:
            print "Please include a filename - save <filename>"
        else:
            success = self.db.save(args.strip())
            if success:
                print "Game saved successfully"
            else:
                print "Unknown error while saving..."
    
    def do_load(self,args):
        """Loads the game from a specified file"""
        if len(args.strip()) == 0:
            print "Please include a filename - load <filename>"
        else:
            backupdb = self.db
            try:
                self.db = GameDB(args.strip())
                print "Loading successful!"
                print
                self.ackMove(MOVE_OK)
                backupdb.end()
            except:
                self.db = backupdb
                print "Loading failed for unknown reasons..."
                
    def do_xyzzy(self,args):
        """..."""
        print "The air shimmers with magic..."
        print "Something big is about to happen..."
        print "..."
        print "You hear a distant sigh of disgust, but nothing else happens."
    
    def default(self,args):
        """For unrecognized commands"""
        
        ret = False
        
        if len(args.strip()) > 0:
            (verb,obj1,obj2) = parseSentence(args)
            #print verb,obj1,obj2
            ret = handle(self.db,verb,obj1,obj2)            
        
        if not ret:
            print getUnkownCmd()
        else:
            print ret
    
    def outputRoom(self):
        print '* '+self.db.getRoomName()+' *'
        print self.db.getRoomDescription()
        
        for item in self.db.getRoomContents():
            print item
        #print
    
    def outputLook(self,dir):
        desc = self.db.getDirection(getSafeDirection(dir))
        if desc:
            print desc
        else:
            print "It's a little foggy..."
                
    def outputLookAt(self,text):
        if text:
            print text
        else:
            print "Excuse me?"            
    
    def precmd(self,line):
        return self.db.clearSynonyms(line)
    
    def postcmd(self,stop,line):
        if stop:
            return stop
        
        if self.db.isEnd():
            for line in self.db.getEnding().splitlines():
                print line
                print "Press ENTER to continue..."
                self.stdin.readline()
            self.db.end()
            return True
        return False
    
    def preloop(self):
        for line in self.db.getIntro().splitlines():
            print line
            print "Press ENTER to continue..."
            self.stdin.readline()
        
        print
        
        for line in self.db.getTitle().splitlines():
            print line
            
        print
        
        self.outputRoom()
        
    def ackMove(self,success=MOVE_OK):
        if success==MOVE_OK:
            self.outputRoom()
        elif success==MOVE_FAILED:
            print 'That way is blocked...'
        else:
            print success