This repository has been archived by the owner on Jul 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
advprompt.py
998 lines (853 loc) · 30 KB
/
advprompt.py
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
#!/usr/bin/env python3
# coding=utf-8
"""Adventure Prompt -- author interactive fiction interactively."""
from __future__ import division
from __future__ import print_function
try:
import readline
except ImportError as e:
print("(Command line editing is unavailable.)\n")
import cmd
import shlex
import json
import uuid
import glob
app_banner = """
Welcome to Adventure Prompt, a system for authoring interactive fiction
interactively, version 2018-03-21. Type HELP or ? to see a list of commands.
"""
help_text = {}
help_text["basics"] = """
Adventure Prompt is a system for authoring interactive fiction interactively,
the same way online building works in a MUSH or MUCK. Walking simulators, or
even simple room escape games, can be created without any scripting, unless
you count the command language itself.
To begin with, type `look` (without the quotes) at the command prompt. Any
game made with Adventure Prompt must contain at least two objects: an actor
with id `hero`, and at least one room for the hero to be in (see: objects).
You can make more objects with the build commands: `dig`, `open`, `create`;
inspect their properties with `look`, `examine` or `say`; and change these
properties with `set` or one of `name`, `desc`, `succ`, `fail`, `drop`,
`lock`, `unlock`. Other commands allow you to save and restore a story file,
clone and recycle objects, move your viewpoint around and so on.
When loaded in an interpreter, the story starts wherever the hero is located
initially; the author's viewpoint is only a convenience for building.
"""
help_text["shortcuts"] = """
The command line supports shortcuts to the most common commands:
- `l` for look;
- `ex` for examine;
- `tel` for teleport.
In addition, typing the name of an object in the current room by itself
will move the author's viewpoint to the exit's destination.
"""
help_text["objects"] = """
Objects are the basic building block of Adventure Prompt story files. Each
story file needs to contain at least two objects in order to be valid:
1. an actor with the ID `hero`, to serve as the player's avatar;
2. a room for the hero to be in.
Each object is recognized by a unique identifier (see: identifiers) and has
at least two properties: a type and a name. The name is what the player sees.
The type determines how the object can be interacted with, and the meaning
of other properties. See: types.
"""
help_text["types"] = """
The editor directly supports four main object types:
- `room` objects contain everything else in the story, even indirectly;
if an object's location (see: properties) can't be traced back to a room,
it's out of play and inaccessible;
- `exit` objects connect rooms together into a navigable grid;
- `actor`: these are the characters of the story -- only the hero for now;
- `thing` denotes objects the player can pick up and carry around.
To get other types, first `create` a thing, then `set` its type to one of:
- `scenery` objects are part of the landscape and not meant to be picked up.
If linked to somewhere else, they become portals that can be entered;
- `vehicle`: the hero can board such an object if they pass the lock. Once
in a vehicle, all exits treat it as the actor for traversal purposes.
- `text`: designates an inscription the player can read if there's light in
the room. Use for something like graffiti, that can't be picked up.
"""
help_text["identifiers"] = """
Identifiers, IDs for short, are character strings by which objects are
stored in the story file. You should keep identifiers short and easy to
type; also, an ID shouldn't start with a "!" (exclamation point), "+"
(plus sign) or "-" (minus sign). For exits, the recommended ID scheme is:
<room id>-<direction>
e.g. `foyer-w` or `bar-n`. Future versions may introduce other restrictions,
such as forcing all IDs to lowercase.
Two IDs are currently reserved: `here` always refers to where the author's
viewpoint is currently located in the editor, and `me` for future use.
"""
help_text["properties"] = """
Most objects support most of the properties below, with different meanings
depending on each object's type:
- string properties: type, name, description, success, failure, drop, nodrop;
- reference properties: link, location;
- numeric properties: score;
- variant properties: lock.
Of these, the editor is aware of type, name, description, location
and link. All others are only meaningful to the story file interpreter.
"""
help_text["success"] = """
The `success` property contains a message to be shown when the object is
successfully used by the player. What that means depends on object type:
- a room is successfully used when seen (and lit);
- an exit is successfully used when traversed;
- a thing is successfully used when picked up.
"""
help_text["linking"] = """
Link is the quick command to set the target of an exit, room, etc. Syntax:
link source-id|here target-id
Most objects can be linked to others. What that means depends on the type:
- for exit objects (the most common case), it's where the exit leads to;
- for room objects, it means where anything dropped in that room ends up
(think being at the top of a tree, versus the ground below);
- a thing linked to something else will make the target not dark when
successfully picked up. Obvious use case: remove a tapestry from a wall
to reveal a secret passage.
"""
help_text["locking"] = """
Lock is the quick command to set the lock of an object. It looks like this:
lock object-id [lock-expression]
where lock-expression is a special character followed by an object ID; the given object is called the key, even though it may not resemble one at all.
- +X checks if X is carried by the player;
- -X checks if X *isn't* carried by the player;
- ?X checks if the player is X, or riding in vehicle X;
- !X checks if the player is *not* (in vehicle) X;
- @X checks if X has been visited;
- ^X checks if X *hasn't* been visited;
- #X checks if X isn't currently dark;
- ~X checks if X *is* dark right now.
When a lock is checked exactly depends on the object type:
- for an exit, when the player tries to traverse it;
- for a thing, when the player tries to pick it up;
- for a vehicle, when the player tries to board it;
- for an inscription, when the player tries to read it.
In MU*s, locks also work for rooms, but that's not yet in Adventure Prompt.
"""
help_text["flags"] = """
- dark
- sticky
- visited
- light
- ending
"""
help_text["dark"] = """
The dark flag applies to most types of objects:
- in a dark room you can't see the room itself or any present objects,
unless a source of light is in scope;
- a dark exit or thing is invisible even in a lit room, and impossible
to interact with.
"""
help_text["sticky"] = """
The sticky flag applies to most types of objects:
- a sticky thing can't be dropped once picked up;
- a sticky room won't let you drop anything inside it;
- a sticky actor will follow the player around once they meet;
- a sticky exit remains unlocked after the lock has been bypassed once.
"""
help_text["ending"] = """
The game ends when an object with the `ending` flag set is successfully
used by the player (see: success). The object's success message will be
shown with appropriate formatting. Currently this only works for rooms,
exits and inscriptions.
"""
help_text["meta"] = """
The Adventure Prompt story file format supports a variety of metadata.
You can manipulate it with the `meta` command, in one of three forms:
meta # Display all the metadata currently set.
meta <field> # Delete given field from the metadata.
meta <field> <value> # Set a new value for the given field.
Currently the interpreter supports the following fields: title*, subtitle,
author*, date, genre, blurb, license, ifid*, language:
- the title and author should always be present; new stories get defaults;
- the date (of first publication) should be a year, or else an ISO date in
yyyy-mm-dd format;
- the language code (e.g. en-US) isn't shown to readers, but the interpreter
passes it on to the web browser;
- the IFID (note: keep the field name in lower case) is a unique identifier
generated for each new story file; if you need another one for any reason,
enter the following command at the prompt:
!print(str(uuid.uuid4()))
"""
# List of recognized keywords, for autocompletion and validation.
meta_keys = ["title", "subtitle", "author", "date",
"genre", "blurb", "license", "ifid", "language"]
object_keys = ["type", "name", "description", "success", "failure",
"drop", "nodrop", "link", "location", "lock",
"dark", "sticky", "visited", "light", "ending"]
config_keys = ["banner", "use_score", "max_score"]
def shell_parse(text):
try:
return shlex.split(text)
except ValueError as e:
print(e)
return []
def parse_value(text):
low = text.lower()
if low in ["true", "yes", "on"]:
return True
elif low in ["false", "no", "off"]:
return False
else:
try:
return float(text)
except ValueError:
return text
except OverflowError:
return text
def new_meta():
return {
"title": "An Interactive Fiction",
"author": "Anonymous",
"ifid": str(uuid.uuid4())
}
def new_config():
return {"banner": "", "max_score": 0, "use_score": True}
def new_room(name):
return {
"type": "room",
"name": name,
"description": ""
}
def new_actor(name, loc=None):
return {
"type": "actor",
"name": name,
"description": "As good-looking as ever.",
"location": loc
}
def new_exit(name, loc=None):
return {
"type": "exit",
"name": name,
"link": None,
"location": loc
}
def new_thing(name, loc=None):
return {
"type": "thing",
"name": name,
"description": "",
"location": loc
}
class Editor(cmd.Cmd):
intro = app_banner
prompt = "\n> "
def __init__(self):
cmd.Cmd.__init__(self)
self.new_game()
self.trash = {}
def new_game(self):
self.game = {
"meta": new_meta(),
"objects": {
"limbo": new_room("Limbo"),
"hero": new_actor("me", "limbo")
},
"config": new_config()
}
self.setprop("limbo", "description", "You are in limbo.")
self.here = "limbo"
self.modified = False
def find(self, prop, val):
obj = self.game["objects"]
return [o for o in obj if obj[o].get(prop) == val]
def examine(self, obj_id):
obj = self.game["objects"][obj_id]
print("Object {0}:".format(obj_id))
for i in obj:
print("{0}: {1}".format(i, obj[i]))
def setprop(self, obj, prop, val):
if obj in self.game["objects"]:
target = self.game["objects"][obj]
if val != False and val != None and val != "":
target[prop] = val
elif prop in target:
del target[prop]
else:
print("No such object: {0}".format(obj))
def goto(self, room):
if room in self.game["objects"]:
self.here = room
self.look(self.here)
else:
print("Can't go to {0}: no such object".format(room))
def look(self, room):
where = self.game["objects"][room]
print("{0} (id: {1})".format(where["name"], room))
print(where["description"])
if "success" in where:
print(where["success"])
allhere = self.find("location", room)
for i in allhere:
obj = self.game["objects"][i]
if obj["type"] == "actor":
print("\n{0} ({1}) is here.".format(
obj["name"], i))
print("\nExits:")
for i in allhere:
obj = self.game["objects"][i]
if obj["type"] == "exit":
print("\t{0} ({1})".format(obj["name"], i))
print("\nYou can see:")
for i in allhere:
obj = self.game["objects"][i]
if obj["type"] not in ["exit", "actor"]:
print("\t{0} ({1})".format(obj["name"], i))
def do_look(self, args):
"""Look at the room you're in, or else the named object."""
args = shell_parse(args)
if len(args) < 1:
self.look(self.here)
elif args[0] in self.game["objects"]:
print(self.game["objects"][args[0]]["description"])
for i in self.game["objects"]:
obj = self.game["objects"][i]
if obj["location"] == args[0]:
print("\t{0} ({1})".format(obj["name"], i))
else:
print("No such object: {0}".format(args[0]))
def do_examine(self, args):
"""List all properties of the named object."""
args = shell_parse(args)
if len(args) < 1:
print('Usage: examine here|object-id')
elif args[0] == "here":
self.examine(self.here)
elif args[0] in self.game["objects"]:
self.examine(args[0])
else:
print("No such object: {0}".format(args[0]))
def do_say(self, args):
"""Print a newline, message, or an object's property value."""
args = shell_parse(args)
if len(args) < 1:
print()
elif len(args) < 2:
print(args[0], end="")
elif args[0] == "here":
room = self.game["objects"][self.here]
if args[1] in room:
print(room[args[1]], end="")
elif args[0] in self.game["objects"]:
obj = self.game["objects"][args[0]]
if args[1] in obj:
print(obj[args[1]], end="")
else:
print("No such object: {0}".format(args[0]))
def do_dig(self, args):
"""Create a new room with given name and ID."""
args = shell_parse(args)
if len(args) < 2:
print('Usage: dig "Room name" room-id')
elif args[1] in self.game["objects"]:
print("ID {0} already in use.".format(args[1]))
elif args[1] == "here" or args[1] == "me":
print("{0} is a reserved word.".format(args[1]))
else:
self.game["objects"][args[1]] = new_room(args[0])
self.modified = True
print("Room created.")
def do_teleport(self, args):
"""Move the viewpoint or an object to another location."""
args = shell_parse(args)
if len(args) < 1:
print('Usage: tel [object-id] here|room-id')
elif len(args) < 2:
if args[0] in self.game["objects"]:
self.here = args[0]
self.look(self.here)
elif args[0] == "here":
print("Nothing to do")
else:
print("No such object: {0}".format(args[0]))
elif args[0] in self.game["objects"]:
if args[1] in self.game["objects"]:
self.setprop(args[0], "location", args[1])
self.modified = True
print("Obj. {0} moved to {1}.".format(*args))
elif args[1] == "here":
self.setprop(args[0], "location", self.here)
self.modified = True
print("Obj. {0} brought here.".format(args[0]))
else:
print("No such object: {0}.".format(args[1]))
else:
print("No such object: {0}.".format(args[0]))
def do_name(self, args):
"""Change the name of a room or other object."""
args = shell_parse(args)
if len(args) < 2:
print('Usage: name here|object-id "New name"')
elif args[0] == "here":
self.setprop(self.here, "name", args[1])
self.modified = True
print("Room name changed.")
elif args[0] in self.game["objects"]:
self.setprop(args[0], "name", args[1])
self.modified = True
print("Name of {0} changed.".format(args[0]))
else:
print("No such object: {0}.".format(args[0]))
def do_desc(self, args):
"""Change the description of a room or other object."""
args = shell_parse(args)
if len(args) < 2:
print('Usage: desc here|object-id "New description"')
elif args[0] == "here":
self.setprop(self.here, "description", args[1])
self.modified = True
print("Room description changed.")
elif args[0] in self.game["objects"]:
self.setprop(args[0], "description", args[1])
self.modified = True
print("Description of {0} changed.".format(args[0]))
else:
print("No such object: {0}.".format(args[0]))
def do_open(self, args):
"""Create an exit to another room at the current location."""
objs = self.game["objects"]
args = shell_parse(args)
if len(args) < 2:
print('Usage: open "Exit name" exit-id [dest-id]')
elif args[1] in objs:
print("Object {0} already exists.".format(args[1]))
elif args[1] == "here" or args[1] == "me":
print("{0} is a reserved word.".format(args[1]))
elif len(args) < 3:
objs[args[1]] = new_exit(args[0], self.here)
self.modified = True
print("Exit created.")
elif args[2] in objs:
objs[args[1]] = new_exit(args[0], self.here)
objs[args[1]]["link"] = args[2]
self.modified = True
print("Exit created and linked.")
else:
print("No such object: {0}.".format(args[2]))
def do_go(self, args):
"""Follow an exit to its destination."""
args = shell_parse(args)
if len(args) < 1:
print('Usage: go exit-name')
return
allhere = self.find("location", self.here)
for i in allhere:
obj = self.game["objects"][i]
if obj["type"] == "exit" and obj["name"] == args[0]:
self.goto(obj["link"])
return
print("You can't go that way.")
def do_link(self, args):
"""Change the destination of an existing exit or room."""
here = self.game["objects"][self.here]
args = shell_parse(args)
if len(args) < 2:
print('Usage: link source-id|here target-id')
elif args[1] not in self.game["objects"]:
print("No such object: {0}.".format(args[1]))
elif args[0] == "here":
here["link"] = args[1]
self.modified = True
print("Room relinked.")
elif args[0] in self.game["objects"]:
self.game["objects"][args[0]]["link"] = args[1]
self.modified = True
print("Object relinked.")
else:
print("No such object: {0}.".format(args[0]))
def do_unlink(self, args):
"""Remove an exit or room destination."""
here = self.game["objects"][self.here]
args = shell_parse(args)
if len(args) < 1:
print('Usage: unlink exit-id|here|room-id')
elif args[0] == "here":
del here["link"]
self.modified = True
print("Room unlinked.")
elif args[0] in self.game["objects"]:
del self.game["objects"][args[0]]["link"]
self.modified = True
print("Object unlinked.")
else:
print("No such object: {0}.".format(args[0]))
def do_lock(self, args):
"""Set a lock on the given object."""
args = shell_parse(args)
if len(args) < 1:
print('Usage 1: lock object-id')
print('Usage 2: lock object-id ?vehicleID')
print('Usage 3: lock object-id !vehicleID')
print('Usage 4: lock object-id +thingID')
print('Usage 5: lock object-id -thingID')
print('Usage 6: lock object-id @thingID')
print('Usage 7: lock object-id ^thingID')
print('Usage 8: lock object-id #thingID')
print('Usage 9: lock object-id ~thingID')
elif args[0] not in self.game["objects"]:
print("No such object: {0}.".format(args[1]))
elif len(args) < 2:
self.setprop(args[0], "lock", "+" + args[0])
self.modified = True
print("Object locked unconditionally.")
elif len(args) < 3:
keys = ["?", "!", "+", "-", "@", "^", "#", "~"]
key_id = args[1][0]
obj_id = args[1][1:]
if obj_id not in self.game["objects"]:
print("No such object: {0}.".format(obj_id))
elif args[0] not in self.game["objects"]:
print("No such object: {0}.".format(args[0]))
elif key_id not in keys:
print("Bad key type: {0}.".format(key_id))
else:
self.setprop(args[0], "lock", args[1])
print("Object locked to given key.")
else:
print("Too many lock expressions.")
def do_unlock(self, args):
"""Remove any lock from the given object."""
args = shell_parse(args)
if len(args) < 1:
print('Usage: unlock object-id')
elif args[0] in self.game["objects"]:
del self.game["objects"][args[0]]["lock"]
self.modified = True
print("Object unlocked.")
else:
print("No such object: {0}.".format(args[0]))
def do_succ(self, args):
"""Change the success message of a room or other object."""
args = shell_parse(args)
if len(args) < 2:
print('Usage: succ here|object-id "New message"')
elif args[0] == "here":
self.setprop(self.here, "success", args[1])
self.modified = True
print("Room success message changed.")
elif args[0] in self.game["objects"]:
self.setprop(args[0], "success", args[1])
self.modified = True
print("Success msg. of {0} changed.".format(args[0]))
else:
print("No such object: {0}.".format(args[0]))
def do_fail(self, args):
"""Change the failure message of a room or other object."""
args = shell_parse(args)
if len(args) < 2:
print('Usage: fail here|object-id "New message"')
elif args[0] == "here":
self.setprop(self.here, "failure", args[1])
self.modified = True
print("Room failure message changed.")
elif args[0] in self.game["objects"]:
self.setprop(args[0], "failure", args[1])
self.modified = True
print("Failure msg. of {0} changed.".format(args[0]))
else:
print("No such object: {0}.".format(args[0]))
def do_drop(self, args):
"""Change the drop message of a room or other object."""
args = shell_parse(args)
if len(args) < 2:
print('Usage: drop here|object-id "New message"')
elif args[0] == "here":
self.setprop(self.here, "drop", args[1])
self.modified = True
print("Room drop message changed.")
elif args[0] in self.game["objects"]:
self.setprop(args[0], "drop", args[1])
self.modified = True
print("Drop msg. of {0} changed.".format(args[0]))
else:
print("No such object: {0}.".format(args[0]))
def do_create(self, args):
"""Create a thing here."""
objs = self.game["objects"]
args = shell_parse(args)
if len(args) < 2:
print('Usage: create "Thing name" thing-id')
elif args[1] in objs:
print("Object {0} already exists.".format(args[1]))
elif args[1] == "here" or args[1] == "me":
print("{0} is a reserved word.".format(args[1]))
else:
objs[args[1]] = new_thing(args[0], self.here)
self.modified = True
print("Thing created.")
def do_clone(self, args):
"""Clone an existing object with a different ID."""
objs = self.game["objects"]
args = shell_parse(args)
if len(args) < 2:
print('Usage: clone old-id new-id')
elif args[0] not in self.game["objects"]:
print("No such object: {0}.".format(args[0]))
elif args[1] in objs:
print("Object {0} already exists.".format(args[1]))
elif args[1] == "here" or args[1] == "me":
print("{0} is a reserved word.".format(args[1]))
else:
objs[args[1]] = objs[args[0]].copy()
self.modified = True
print("Object cloned.")
def do_set(self, args):
"""Set a certain flag or property on a given object."""
objs = self.game["objects"]
args = shell_parse(args)
if len(args) < 2:
print('Usage 1: set object-id [!]flag-name')
print('Usage 2: set object-id prop-name prop-value')
elif args[0] not in objs:
print("No such object: {0}.".format(args[0]))
elif len(args) < 3:
if args[1][0] != "!":
self.setprop(args[0], args[1], True)
print("Flag set.")
else:
self.setprop(args[0], args[1][1:], False)
print("Flag reset.")
self.modified = True
else:
self.setprop(args[0], args[1], parse_value(args[2]))
print("Property changed.")
self.modified = True
def do_find(self, args):
"""Find all objects with a certain flag or property."""
objs = self.game["objects"]
args = shell_parse(args)
if len(args) < 1:
print('Usage 1: find [!]flag-name')
print('Usage 2: find prop-name prop-value')
elif len(args) < 2:
if args[0][0] != "!":
found = self.find(args[0], True)
else:
found = self.find(args[0][1:], False)
for i in found:
name = objs[i]["name"]
print("{0} (id: {1})".format(name, i))
else:
found = self.find(args[0], args[1])
for i in found:
name = objs[i]["name"]
print("{0} (id: {1})".format(name, i))
def do_recycle(self, args):
"""Move an object to the recycle bin."""
args = shell_parse(args)
if len(args) < 1:
print('Usage: recycle object-id')
elif args[0] == "here" or args[0] == self.here:
print("Can't recycle the room you're in right now.")
elif args[0] == "hero":
print("Can't recycle the hero of the story.")
elif args[0] == self.game["objects"]["hero"]["location"]:
print("Can't recycle the hero's current location.")
elif args[0] in self.game["objects"]:
self.trash[args[0]] = self.game["objects"][args[0]]
del self.game["objects"][args[0]]
self.modified = True
print("Object moved to recycle bin.")
def do_unrecycle(self, args):
"""Bring an object back from the recycle bin."""
args = shell_parse(args)
if len(args) < 1:
print('Usage: unrecycle object-id')
elif args[0] in self.trash:
self.game["objects"][args[0]] = self.trash[args[0]]
del self.trash[args[0]]
self.modified = True
print("Object brought back from recycle bin.")
else:
print("No such object in the recycle bin.")
def do_meta(self, args):
"""List or change game metadata such as title and author."""
args = shell_parse(args)
if len(args) < 1:
meta = self.game["meta"]
for i in meta:
print("{0}: {1}".format(i, meta[i]))
elif len(args) < 2:
if args[0] in self.game["meta"]:
del self.game["meta"][args[0]]
self.modified = True
print("Field deleted.")
else:
print("No such field; nothing to do.")
else:
self.game["meta"][args[0]] = args[1]
self.modified = True
print("Field value changed.")
def do_config(self, args):
"""List or change configuration settings like the banner."""
config = self.game["config"]
args = shell_parse(args)
if len(args) < 1:
for i in config:
print("{0}: {1}".format(i, config[i]))
elif len(args) < 2:
if args[0] in config:
print("{0}: {1}".format(
args[0], config[args[0]]))
else:
print("No such setting.")
else:
self.game["config"][args[0]] = parse_value(args[1])
self.modified = True
print("Field value changed.")
def do_new(self, args):
"""Start over with a blank game."""
args = shell_parse(args)
if not self.modified:
self.new_game();
print("New game initialized.")
elif len(args) > 0 and args[0] == "forced":
self.new_game();
print("New game initialized.")
else:
print("You have an unsaved game in progress.")
print("Type NEW FORCED to override.")
def do_save(self, args):
"""Save current game to disk."""
args = shell_parse(args)
if len(args) < 1:
print('Usage: save <filename>')
else:
try:
with open(args[0], "w") as f:
json.dump(self.game, f)
self.modified = False
print("Game saved.")
except Exception as e:
print("Couldn't save game: " + str(e))
def do_restore(self, args):
"""Restore a game from disk."""
args = shell_parse(args)
if len(args) < 1:
print('Usage: restore <filename>')
else:
try:
with open(args[0], "r") as f:
game = json.load(f)
# TO DO: sanity checks
self.game = game
self.here = game["objects"]["hero"]["location"]
print("Game restored.")
except Exception as e:
print("Couldn't restore game: " + str(e))
def do_quit(self, args):
"""Quit the editor and return to the operating system."""
args = shell_parse(args)
if not self.modified:
return True
elif len(args) > 0 and args[0] == "forced":
return True
else:
print("You have an unsaved game in progress.")
print("Type QUIT FORCED to override.")
def do_shell(self, args):
"""Run Python code in a global context, for debugging."""
try:
exec(args, globals())
except Exception as e:
print(e)
def default(self, line):
args = shell_parse(line)
if args[0] == "l":
if len(args) < 2:
return self.do_look("")
else:
return self.do_look(args[1])
elif args[0] == "tel":
if len(args) < 2:
return self.do_teleport("")
elif len(args) < 3:
return self.do_teleport(args[1])
else:
return self.do_teleport(
args[1] + " " + args[2])
elif args[0] == "ex":
return self.do_examine(args[1])
elif len(args) == 1:
return self.do_go(args[0])
else:
print("Unknown command: {0}.".format(args[0]))
def complete_name(self, text, line, begidx, endidx):
return [i for i in self.game["objects"] if i.startswith(text)]
def complete_desc(self, text, line, begidx, endidx):
return [i for i in self.game["objects"] if i.startswith(text)]
def complete_succ(self, text, line, begidx, endidx):
return [i for i in self.game["objects"] if i.startswith(text)]
def complete_fail(self, text, line, begidx, endidx):
return [i for i in self.game["objects"] if i.startswith(text)]
def complete_drop(self, text, line, begidx, endidx):
return [i for i in self.game["objects"] if i.startswith(text)]
def complete_link(self, text, line, begidx, endidx):
return [i for i in self.game["objects"] if i.startswith(text)]
def complete_unlink(self, text, line, begidx, endidx):
return [i for i in self.game["objects"] if i.startswith(text)]
def complete_lock(self, text, line, begidx, endidx):
return [i for i in self.game["objects"] if i.startswith(text)]
def complete_unlock(self, text, line, begidx, endidx):
return [i for i in self.game["objects"] if i.startswith(text)]
def complete_clone(self, text, line, begidx, endidx):
return [i for i in self.game["objects"] if i.startswith(text)]
def complete_look(self, text, line, begidx, endidx):
return [i for i in self.game["objects"] if i.startswith(text)]
def complete_examine(self, text, line, begidx, endidx):
return [i for i in self.game["objects"] if i.startswith(text)]
def complete_teleport(self, text, line, begidx, endidx):
return [i for i in self.game["objects"] if i.startswith(text)]
def complete_recycle(self, text, line, begidx, endidx):
return [i for i in self.game["objects"] if i.startswith(text)]
def complete_unrecycle(self, text, line, begidx, endidx):
return [i for i in self.trash if i.startswith(text)]
def complete_find(self, text, line, begidx, endidx):
return [i for i in object_keys if i.startswith(text)]
def complete_meta(self, text, line, begidx, endidx):
return [i for i in meta_keys if i.startswith(text)]
def complete_config(self, text, line, begidx, endidx):
return [i for i in config_keys if i.startswith(text)]
def complete_save(self, text, line, begidx, endidx):
return glob.glob(text + "*")
def complete_restore(self, text, line, begidx, endidx):
return glob.glob(text + "*")
def help_basics(self):
print(help_text["basics"])
def help_shortcuts(self):
print(help_text["shortcuts"])
def help_objects(self):
print(help_text["objects"])
def help_types(self):
print(help_text["types"])
def help_identifiers(self):
print(help_text["identifiers"])
def help_rooms(self):
print("To be done.")
def help_exits(self):
print("To be done.")
def help_link(self):
print(help_text["linking"])
def help_lock(self):
print(help_text["locking"])
def help_properties(self):
print(help_text["properties"])
def help_flags(self):
print(help_text["flags"])
def help_ending(self):
print(help_text["ending"])
def help_success(self):
print(help_text["success"])
def help_dark(self):
print(help_text["dark"])
def help_sticky(self):
print(help_text["sticky"])
def help_visited(self):
print("To be done.")
def help_meta(self):
print(help_text["meta"])
if __name__ == "__main__":
editor = Editor()
editor.cmdloop()