-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwizards-castle.lisp
4439 lines (3822 loc) · 160 KB
/
wizards-castle.lisp
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
999
1000
;;;; -*- mode: lisp; coding: utf-8 -*-
;;;; FILE: wizard.lisp
;;;; DESCRIPTION:
;;;; Wizard's Castle
;;;; Copyright (C) 1980 Joseph Power
;;;; Last revised - 4/12/80 11:10 PM
;;;; Adapted to Common Lisp by William Clifford
;;;; AUTHORS:
;;;; William Clifford [wc] wobh@yahoo.com
;;;; NOTES:
;;;; I intended to make it easier for extending the game and adding
;;;; options and features not originally available. However, I fear
;;;; what I've done is make a fairly baroque program with several
;;;; abstractions which may not be obviously useful.
;;;; See the README.org for more information.
;;;; MAKE-WIZ-FORM creates a lisp-like form which is evaluated in one
;;;; of the MAIN-EVAL or FIGHT-EVAL. Evaluated, it should return a
;;;; HISTORY of events and, optionally a message.
;;;; MAKE-EVENT makes an event which is a lisp-like list describing
;;;; the changes in game-state.
;;;; MAKE-HISTORY makes a history of events, which currently is just a
;;;; list of the arguments in reverse order. Arguments, when present,
;;;; must be valid events by EVENT-P.
;;;; JOIN-HISTORY is a way of extending a given event-history with a
;;;; history of additional events. Any wiz-form calling on other
;;;; wiz-forms should use JOIN-HISTORY to merge it's current events
;;;; with ones from other wiz-forms.
;;;; MAKE-TEXT and PUSH-TEXT do similiar work for strings. Any
;;;; wiz-form calling on other wiz-forms should use PUSH-TEXT to
;;;; extent the current message.
;;;; with the exception of WIZ-ERROR messages, evaluators of wizforms
;;;; will output the final message and join the form history to the
;;;; castle history. See MAIN-EVAL.
;;;; To extend the game you'll want to make OUTCOMES, (not yet
;;;; documented, see examples) and create the appropriate forms.
;;;; REFERENCES:
;;;; Power, Joseph R.; Wizard's Castle; Recreational Computing; 1980,
;;;; July-August pgs 10-17
;;;; O'Hare, John; Wizard's Castle; Baf's guide to the Interactive
;;;; Fiction Archive; http://www.wurb.com/if/index; page:
;;;; http://www.wurb.com/if/game/678
;;;; Stetson, J.F.; Wizard's Castle; Baf's guide to the Interactive
;;;; Fiction Archive; http://www.wurb.com/if/index; page:
;;;; http://www.wurb.com/if/game/678
;;;; Licht, Derell; Wizard's Castle;
;;;; http://home.comcast.net/~derelict/winwiz.html
;;;; Interview with Joseph Power:
;;;; http://www.armchairarcade.com/neo/node/1381
(defpackage #:org.wobh.common-lisp.games.wizards-castle
(:nicknames #:wizards-castle #:wizard #:zot)
(:use #:common-lisp)
(:export #:play #:play-ohare #:play-stetson)
(:documentation "ORG.WOBH.COMMON-LISP.GAMES.WIZARDS-CASTLE
Joseph Power's _Wizard's Castle_"))
(in-package #:org.wobh.common-lisp.games.wizards-castle)
;;;; Randomess functions
;;; Powers, 1980, pg 14, ln# 70
;;; DEFFNA(Q)=1+INT(RND(8)*Q)
(defun random-array-subscripts (array &optional (random-state *random-state*))
"Create a list random subscripts in array."
(flet ((randomn (n)
(random n random-state)))
(mapcar #'randomn
(array-dimensions array))))
(defun random-aref (array &optional (random-state *random-state*))
"Get random element of array."
(row-major-aref array
(random (array-total-size array)
random-state)))
(defun random-elt (seq &optional (random-state *random-state*))
"Get random element from sequence."
(let ((seq-len (length seq)))
(unless (zerop seq-len)
(elt seq (random seq-len random-state)))))
(defun random-whole (limit &optional (random-state *random-state*))
"Return random integer n, 0 < n <= `limit'."
(1+ (random limit random-state)))
(defun random-range (min max &optional (random-state *random-state*))
"Return random interger n `min' <= n <= `max'."
(+ min (random (- max min) random-state)))
(defun nshuffle (seq &optional (random-state *random-state*))
"Destructively Knuth shuffle a sequence."
(let ((len (length seq)))
(dotimes (i len seq)
(rotatef (elt seq i)
(elt seq (+ i (random (- len i) random-state)))))))
(defun shuffle (seq &optional (random-state *random-state*))
"Non-destructively Knuth shuffle a sequence."
(nshuffle (copy-seq seq) random-state))
;;; Knuth shuffle
;;; https://groups.google.com/d/topic/comp.lang.lisp/1ZtO84hrAuM/discussion
;;;; Castle accessors
;;; Powers, 1980, pg 13
;;; "FNB(Q) = Q + 8 * ((Q = 9) - (Q = 0)) <- causes wraparound at borders"
;;; Powers, 1980, pg 14, ln# 70
;;; DEFFNB(Q)=Q+8((Q=9)-(Q=0))
;;; Powers describes Zot's castle as an 8x8x8 manifold, like 8 nested
;;; donuts ("torus") in which falling from the inmost means landing on
;;; the outermost (1980, pg 11). To affect this, here are some array
;;; access functions that apply a modulus to their subscripts.
(defun mrray-in-bounds-p (array &rest subscripts)
"Check that number of subscripts match array rank."
(= (length subscripts) (array-rank array)))
(defun mref (array &rest subscripts)
"Access array with modulus on subscripts for manifold-like access."
(when (apply #'mrray-in-bounds-p array subscripts)
(apply #'aref array
(map-into subscripts #'mod subscripts
(array-dimensions array)))))
(defun set-mref (array subscripts value)
"Set array element with modulus applied to subscripts."
(when (apply #'mrray-in-bounds-p array subscripts)
(setf (apply #'aref array
(map-into subscripts #'mod subscripts
(array-dimensions array)))
value))
;; FIXME: (maybe) I'm using MAP-INTO instead of MAPCAR so that it
;; will give an error when given too many subscripts as well as too
;; few.
)
(defsetf mref set-mref)
(defun row-major-mref (array index)
"Access an array from an index modulated to the array's size."
(row-major-aref array (mod index (array-total-size array))))
(defun mrray-row-major-index (array &rest subscripts)
"Make an index for array from modulated subscripts."
(when (apply #'mrray-in-bounds-p array subscripts)
(apply #'array-row-major-index array
(map-into subscripts #'mod subscripts (array-dimensions array)))))
;;; Powers, 1980, pg 15, ln# 50:
;;; DEFFND(Q)=Q*64+X*8+Y-585
;;; Powers, 1980, pg 13:
;;; "computes room location in memory [...] After 32767, memory
;;; locations (for POKE and PEEK commands are numbered -32768 (8000
;;; hex) to -1 (FFFF hex)."
;;; Actually we might need the opposite. David D. Smith's third post
;;; in "iteration over multidimensional array" is a function that
;;; turns an index into array subscripts.
;;; http://groups.google.com/group/comp.lang.lisp/msg/bca19f4a3d0a5e3e
(defun array-index-row-major (array index)
"Turn a row-major-index back into subcripts for the array."
(row-major-aref array index) ; trigger error if index out of range
(flet ((trunconc (elt acc)
(nconc
(multiple-value-list (truncate (car acc) elt))
(cdr acc))))
(reduce #'trunconc
(cdr (array-dimensions array))
:initial-value (list index)
:from-end t)))
;;; array filter
(defun filter-array-indices (predicate an-array)
"Make a list of array indices which elements pass some test"
(loop
for index from 0 below (array-total-size an-array)
when (funcall predicate (row-major-aref an-array index))
collect index))
;;;; TODO mimic display output?
;;; - http://oldcomputers.net/
;;; - http://www.old-computers.com
;;; - http://www.computer-museum.nl
;;; | make | model | ch-width | ch-height | px-width | px-height |
;;; |-----------+----------+----------+-----------+----------+-----------|
;;; | Exidy | Sorcerer | 64 | 30 (32?) | 512 | 240 |
;;; | Commodore | Pet | 40 | 25 | | |
;;; | Apple | II | 40 | 24 | 280 | 192 |
;;; | Commodore | Vic-20 | 22 | 23 | 176 | 184 |
;;; | Commodore | 64 | 40 | 25 | | |
;;; | MS | DOS | 80 | 25 | | |
;; (defstruct console text-width text-height)
;; (defparameter *consoles*
;; (list
;; :exidy-sorcerer (make-console :text-width 64 :text-height 30)
;; :commodore-pet (make-console :text-width 40 :text-height 25)
;; :microsoft-dos (make-console :text-width 80 :text-height 25)))
;; Some sources say the Exidy Sorcerer had a text-height of 32.
;; (defun get-console (computer)
;; (getf *consoles* (intern (symbol-name computer) 'keyword)))
;; (defparameter *platform* (get-console 'exidy-sorcerer))
;;;; Input and output
(defparameter *all-caps*
"~:@(~@?~)"
"Format string for WIZ-FORMAT when ZOT SPEAKS IN ALL CAPS")
(defparameter *mixed-case*
"~@?"
"Format string for WIZ-FORMAT when Zot speaks normally")
(defparameter *wiz-format-string*
*all-caps*
;; +mixed-case+ ; for a less obnoxious Zot
"Format string for WIZ-FORMAT.")
;;; FIXME: feels a bit like reinventing the wheel here
(defparameter *wiz-width* 64)
(defparameter *wiz-out* *standard-output*)
(defparameter *wiz-err* *standard-output*)
(defun wiz-format (stream str &rest args)
"Format a string for output in wizard's castle."
(apply #'format stream *wiz-format-string* str args))
(defun wiz-format-error (stream string &rest args)
"Write a formatted error message to STREAM."
(wiz-format stream "~2&** ~?" string args))
(defun wiz-read-char (&optional (stream *standard-input*))
"Read the upcased first character from whatever entered."
(char-upcase (read-char stream)))
(defun wiz-read-n (&optional (stream *standard-input*))
"Read a number from whatever entered."
(parse-integer (read-line stream) :junk-allowed t))
(defun make-prompt-adv-choice (&optional intro)
"Make a prompt with 'Your choice' at the end."
(let ((prompt "Your choice "))
(if intro
(format nil "~2&~A~2&~(~A~)" intro prompt)
(format nil "~2&~A" prompt))))
(defmacro with-player-input
((var prompt &key
(readf '#'wiz-read-char) (istream '*standard-input*)
(writef '#'wiz-format) (ostream '*standard-output*))
&body body)
"Read input to var and do something with it or set var to nil if a
different input is needed."
(let ((out (gensym "OUTCOME")))
`(loop
with ,var = nil
with ,out = nil
do
(funcall ,writef ,ostream ,prompt)
(finish-output ,ostream)
(clear-input ,istream)
(setf ,var (funcall ,readf ,istream))
(setf ,out ,@body)
until (not (null ,var))
finally (return ,out))))
(defun wiz-y-or-n-p (prompt &optional message)
"Return t, nil or requery if answer is Y, N, or something else."
(when message
(wiz-format *wiz-out* message))
(with-player-input (input prompt :readf #'wiz-read-char)
(case input
(#\Y t)
(#\N nil)
(t (setf input
(wiz-format-error *wiz-err*
"Answer yes or no "))))))
(defun wiz-y-p (prompt &optional message)
(when message
(wiz-format *wiz-out* message))
(with-player-input (input prompt :readf #'wiz-read-char)
(case input
(#\Y t)
(t nil))))
(defun wiz-read-direction (prompt &optional input-error-message)
"Read a direction from the player.
INPUT-ERROR-MESSAGE provides an error message and loops, otherwise, it
returns INPUT-ERROR."
(with-player-input (direction prompt)
(case direction
(#\N 'north)
(#\E 'east)
(#\W 'west)
(#\S 'south)
(t (if input-error-message
(setf direction
(wiz-format-error *wiz-err*
input-error-message))
'input-error)))))
;;; ASCII controls used in code:
;;; | DEC | CHR | Description | character | Format |
;;; |-----+-----+-----------------+-----------+---------|
;;; | 007 | BEL | Bell | | |
;;; | 010 | NL | Newline | #\Newline | ~& |
;;; | 012 | FF | Formfeed | #\Page | ~| |
;;; | 013 | CR | Carriage Return | #\Return | ~% |
;;;; Events, Turns, and History
;;; Events
(defparameter *events*
'(adv-ate
adv-drank-potion
adv-entered-castle
adv-entered-room adv-found
adv-mapped adv-viewed-map
adv-walked adv-teleported
adv-used adv-tried adv-opened adv-drank
adv-warped adv-fell adv-staggered
adv-attacked adv-wounded adv-cast-spell adv-bribed adv-retreated
foe-attacked foe-wounded foe-bound foe-slain foe-bribed foe-unbound
adv-strike-hit adv-strike-missed
adv-blinded adv-bound
adv-cured adv-unbound
adv-bought adv-sold adv-ignored
adv-dozed adv-forgot
adv-gained adv-lost
adv-donned adv-doffed
adv-wielded
adv-armor-damaged adv-weapon-broke adv-armor-destroyed
adv-changed
trap-sprang
chest-expoded
adv-left-castle
adv-slain
player-quit-game
player-error
player-viewed-help)
"List of events")
(defun event-p (obj)
"Verify that the object is an event."
(find
(typecase obj
(list (first obj))
(t obj))
*events*))
(defun make-event (&rest args)
"An event is a list describing what happened."
(assert (find (first args) *events*))
args)
(defun make-event* (&rest args)
(assert (find (first args) *events*))
(apply #'list* args))
;;(defun name-of-event (event)
(defun name-of-event (event)
"Get event type info."
(assert (event-p event))
(first event))
;;(defun event-name-p (event name-ref)
(defun event-kind-p (event-check event)
"Check that an event has a particular properties."
(assert (event-p event))
(etypecase event-check
(symbol (eql event-check (first event)))
(list (let ((index (mismatch event event-check)))
(or (null index)
(= index (length event-check)))))))
(defun data-of-event (event &optional data-ref)
"Get data about event."
(assert (event-p event))
(let ((subtype (rest event)))
(etypecase data-ref
(null subtype)
(symbol (member data-ref subtype))
((integer 0) (subseq subtype data-ref)))))
(defun value-of-event (event &optional value-ref)
"Get most specific information about event"
(assert (event-p event))
(etypecase value-ref
(null (first (last event)))
(symbol (rest (member value-ref event)))
((integer 0) (last event value-ref))))
;;; History
(defun history-p (events-list)
"Is every element of EVENTS-LIST an event?"
(or nil (every #'event-p events-list)))
(defun make-history (&rest events)
"A history is push-down stack of events (returns reversed list of events)."
(assert (history-p events))
(reverse events))
(define-modify-macro record-event (event)
(lambda (history event)
(assert (event-p event))
(push event history))
"Record an event to history.")
(define-modify-macro record-events (&rest events)
(lambda (history &rest events)
(setf history (revappend events history)))
"Record events to history.")
(define-modify-macro join-history (new-history)
(lambda (old-history new-history)
(setf old-history (append new-history old-history)))
;; (dolist (history histories old-history)
;; (setf old-history (revappend history old-history))))
"Combine histories into the old history.")
(defun count-events (history)
"Count the number of turns in history."
(count-if #'event-p history :key 'first))
(defun events-since (event-check history)
"History since event."
(assert (event-p event-check))
(let ((index (position event-check history :test #'event-kind-p)))
(when (typep index '(integer 0))
(butlast history (- (length history) (1+ index))))))
(defun find-event (event-check history)
"Search history for event."
(flet ((event-check-p (event)
(event-kind-p event-check event)))
(find-if #'event-check-p history)))
(defun latest-event (history)
"Last event in history."
(first history))
(defun latest-event-p (event-check history)
"Is the latest event in history the kind of event expected?"
(if (null history)
nil
(event-kind-p event-check (latest-event history))))
(defun oldest-event (history)
"First event in history."
(first (last history)))
;; FIXME: no one calls this
(defun oldest-event-p (event-check history)
"Is the oldest event in history the kind of event expected?"
(event-kind-p event-check (oldest-event history)))
(defun latest-creature-found (history)
"What creature has been most recently found."
(value-of-event
(oldest-event
(events-since 'adv-found history))))
(defparameter *command-events*
'((adv-drank pool)
adv-walked
(adv-used map)
(adv-used flare)
(adv-used lamp)
(adv-used crystal-orb)
(adv-used runestaff)
(adv-opened book)
(adv-opened chest)
player-quit)
"List of events generated by commands")
;;; TODO: eventually this list will become the commands by threading
;;; the castle as the first argument eg (funcall adv-walked castle
;;; 'west)
;;; Turns (a special kind of event)
(defparameter *turn-events*
'(adv-drank adv-walked adv-used adv-opened adv-viewed-map adv-dozed)
"List of events counted as turns.")
(defun turn-p (obj)
"Is the given event a turn?"
(assert (event-p obj))
(find
(etypecase obj
(symbol obj)
(list (first obj)))
*turn-events*))
(defun count-turns (history)
"Count the number of turns in history."
(count-if #'turn-p history :key 'first))
;;;; Messages
(defun make-text (&rest strings)
(apply #'concatenate 'string strings))
(define-modify-macro push-text (text)
(lambda (message text)
(setf message (make-text message text)))
"Push a text onto a message.")
;;;; Show title screen
(defparameter *wiz-title-plain*
"THE WIZARD'S CASTLE")
(defparameter *wiz-title-fancy*
(format nil "* * * ~A * * *" *wiz-title-plain*))
(defparameter *wiz-title*
*wiz-title-plain*)
(defparameter *wiz-copyright-power*
"Copyright (C) 1980 by Joseph R Power")
(defparameter *wiz-copyright*
*wiz-copyright-power*)
(defparameter *wiz-revision-power*
"Last Revised - 04/12/80")
(defparameter *wiz-revision-ohare*
"Last Revised - 07/09/80 12:30 AM")
(defparameter *wiz-revision*
*wiz-revision-power*)
(defparameter *wiz-adaptation-ohare*
"Converted to PET by - John O'Hare")
(defparameter *wiz-adaptation*
nil)
(defparameter *intro-text-dos*
(format nil
"Many cycles ago, in the kingdom of N'DIC, the gnomic~
~&wizard ZOT forged his great ORB of power. He soon vanished~
~&utterly, leaving behind his vast subterranean castle~
~&filled with esurient MONSTERS, fabulous TREASURES, and~
~&the incredible ORB of ZOT. From that time hence, many~
~&a bold youth has ventured into the WIZARD'S CASTLE. As~
~&of yet, NONE has ever emerged victoriously! BEWARE!!")
"This intro is a slightly modified version of the article's introduction.")
(defparameter *intro-text-ohare*
(format nil
"codes:~
~@{~&~A=~A~20T~A=~A~}"
#\. "an empty room"
#\e "the entrance"
#\u "stairs going up"
#\d "stairs going down"
#\p "a pool"
#\c "a chest"
#\g "gold pieces"
#\f "flares"
#\w "a warp"
#\s "a sinkhole"
#\o "a crystal orb"
#\b "a book"
#\v "vendor"
#\m "any monster"
#\t "any treasure"
#\? "unexplored")
)
(defparameter *wiz-intro* nil
"Original game does not print an into like some later ones.")
(defun make-message-launch (&key
(width *wiz-width*)
(title *wiz-title*)
(copyright *wiz-copyright*)
(revision *wiz-revision*)
(adaptation *wiz-adaptation*)
(introduction *wiz-intro*))
"Print title screen"
(let ((stars (make-string width :initial-element #\*))
(indent (1- (floor (- width (length title)) 2))))
(with-output-to-string (message)
(format message "~&~|~&")
(format message "~&~A" stars)
(format message "~2&~VT~A" indent title)
(format message "~2&~A" stars)
(format message "~@[~2&~A~]" copyright)
(format message "~@[~2&~A~]" revision)
(format message "~@[~2&~A~]" adaptation)
(format message "~@[~2&~A~]" introduction)
(format message "~%"))))
;; FIXME what should I do about form-feed (CHR$(12)? clear screen CLS?
;; Seems likely the revision date here means 1980-04-12
(defun launch ()
(wiz-format *wiz-out* "~A" (make-message-launch)))
;;; TODO: filter list by contents of another list
;;;; Zot's creatures
;;; the documentation strings are from (Power 1980), pg 13
;;; FIXME: perhaps some of the parameters should be constants?
;;; FIXME: the first item on these lists is nil because the original
;;; basic source code used natural number indexes. To maintain
;;; consistency with the code, and prevent confusion when checking
;;; against it, the zeroth element has been set to nil.
;; | n | symbol | icon| text | type |
;; |----|-------------+-----+---------------------|----------|
;; | 0 | nil | nil | nil | nil |
;; | 1 | empty-room | #\. | "an empty room" | empty |
;; | 2 | entrance | #\e | "the entrance" | entrance |
;; | 3 | stairs-up | #\u | "stairs going up" | stairs |
;; | 4 | stairs-down | #\d | "stairs going down" | stairs |
;; | 5 | pool | #\p | "a pool" | room |
;; | 6 | chest | #\c | "a chest" | room |
;; | 7 | gold | #\g | "gold pieces" | room |
;; | 8 | flares | #\f | "flares" | room |
;; | 9 | warp | #\w | "a warp" | room |
;; | 10 | sinkhole | #\s | "a sinkhole" | room |
;; | 11 | crystal-orb | #\o | "a crystal orb" | room |
;; | 12 | book | #\b | "a book" | room |
;; | 13 | kobold | #\m | "a kobold" | monster |
;; | 14 | orc | #\m | "an orc" | monster |
;; | 15 | wolf | #\m | "a wolf" | monster |
;; | 16 | goblin | #\m | "a goblin" | monster |
;; | 17 | ogre | #\m | "an ogre" | monster |
;; | 18 | troll | #\m | "a troll" | monster |
;; | 19 | bear | #\m | "a bear" | monster |
;; | 20 | minotaur | #\m | "a minotaur" | monster |
;; | 21 | gargoyle | #\m | "a gargoyle" | monster |
;; | 22 | chimera | #\m | "a chimera" | monster |
;; | 23 | balrog | #\m | "a balrog" | monster |
;; | 24 | dragon | #\m | "a dragon" | monster |
;; | 25 | vendor | #\v | "a vendor" | vendor |
;; | 26 | ruby-red | #\t | "the ruby red" | treasure |
;; | 27 | norn-stone | #\t | "the norn stone" | treasure |
;; | 28 | pale-pearl | #\t | "the pale pearl" | treasure |
;; | 29 | opal-eye | #\t | "the opal eye" | treasure |
;; | 30 | green-gem | #\t | "the green gem" | treasure |
;; | 31 | blue-flame | #\t | "the blue flame" | treasure |
;; | 32 | palantir | #\t | "the palantir" | treasure |
;; | 33 | silmaril | #\t | "the silmaril" | treasure |
;; | 34 | x | #\? | "x" | ? |
(defparameter *creature-data*
'((x "x" #\?)
(empty-room "an empty room" #\.)
(entrance "the entrance" #\e)
(stairs-up "stairs going up" #\u)
(stairs-down "stairs going down" #\d)
(pool "a pool" #\p)
(chest "a chest" #\c)
(gold-pieces "gold pieces" #\g)
(flares "flares" #\f)
(warp "a warp" #\w)
(sinkhole "a sinkhole" #\s)
(crystal-orb "a crystal orb" #\o)
(book "a book" #\b)
(kobold "a kobold" #\m)
(orc "an orc" #\m)
(wolf "a wolf" #\m)
(goblin "a goblin" #\m)
(ogre "an ogre" #\m)
(troll "a troll" #\m)
(bear "a bear" #\m)
(minotaur "a minotaur" #\m)
(gargoyle "a gargoyle" #\m)
(chimera "a chimera" #\m)
(balrog "a balrog" #\m)
(dragon "a dragon" #\m)
(vendor "a vendor" #\v)
(ruby-red "the Ruby Red" #\t)
(norn-stone "the Norn Stone" #\t)
(pale-pearl "the Pale Pearl" #\t)
(opal-eye "the Opal Eye" #\t)
(green-gem "the Green Gem" #\t)
(blue-flame "the Blue Flame" #\t)
(palantir "the Palantir" #\t)
(silmaril "the Silmaril" #\t)
(runestaff "the Runestaff" nil)
(orb-of-zot "the Orb of Zot" nil)
)
"All the possible castle contents")
(defun creature-p (creature)
"Is the given symbol a creature?"
(find creature (subseq *creature-data* 1 34) :key 'first))
;; (defun creature-data (creature-ref)
(defun get-creature-data (creature-ref)
"Return the requested data about the creature."
(let ((creature-data (find creature-ref *creature-data* :key #'first)))
(assert (consp creature-data))
creature-data))
;; (defun creature-value (creature-ref)
(defun value-of-creature (creature-ref)
"Get the creature number."
(position creature-ref *creature-data* :key #'first))
;; (creature-text (creature-ref)
(defun text-of-creature (creature-ref)
"Get the creature text."
(second (get-creature-data creature-ref)))
;; (creature-icon (creature-ref)
(defun icon-of-creature (creature-ref)
"Get the creature map icon."
(string (third (get-creature-data creature-ref))))
(defun icon-of-unmapped ()
"Get the icon for a unmapped room."
(icon-of-creature 'x))
;;;; Directions and vectors in Castle Zot
(defparameter *directions*
'((down (1 0 0))
(up (-1 0 0))
(south (0 1 0))
(north (0 -1 0))
(east (0 0 1))
(west (0 0 -1))))
(defun direction-p (symbol)
(find symbol *directions* :key #'first))
(defun name-of-vector (vector)
(first (find vector *directions* :key #'second)))
(defun vector-of-direction (direction)
(assert (direction-p direction))
(second (find direction *directions* :key #'first)))
(defun map-manifold-vectors (function manifold &rest vectors)
"Map function over manifold vectors, modulus dimensions of the manifold."
(mapcar #'mod
(apply #'mapcar function vectors)
(array-dimensions manifold)))
;;; The original coordinate system reversed what I would call the X
;;; and Y axes and also counted up from 1.
;;; I judge the interest of coherence outweighs fidelity in this
;;; case. We will count from zero and use "normal" x and y
;;; internally. We'll translate into zot coords for the user interface.
;;; NOTE:
;;; (aref array-3d x y z)
;;; (array-row-major-index array-3d z y x)
(defparameter *cas-coords* :zot
"What style to display castle coordinates in.")
(defun wiz-coords (coords)
"Arrange coordinates in the order and value expected."
(ecase *cas-coords*
(:array (reverse coords))
(:zot (map 'list #'1+
(list (second coords) (third coords) (first coords))))))
(defun unwiz-coords (wizd-coords)
"Arrange coordinates from original game order and values to internal
order and values."
(ecase *cas-coords*
(:array (reverse wizd-coords))
(:zot (map 'list #'1-
(list (third wizd-coords)
(first wizd-coords)
(second wizd-coords))))))
;;;; Locations and creatures in castle Zot.
(defparameter *zot-castle-dimensions* '(8 8 8)
"Dimensions of castle.")
(defun make-castle-rooms ()
"Make an array for storing castle room data."
(make-array *zot-castle-dimensions*
:element-type 'symbol :initial-element 'empty-room))
(defun castle-height (castle-rooms)
"How tall is the castle?"
(first (last (array-dimensions castle-rooms) 3)))
(defun castle-level-dimensions (castle-rooms)
"What are the dimensions of the castle levels?"
(last (array-dimensions castle-rooms) 2))
(defun calc-castle-level-offset (level castle-rooms)
"Calculate the index offset for castle-levels"
(reduce #'* (list* level (castle-level-dimensions castle-rooms))))
;;; FIXME [wc 2012-12-26] It seems like it would be good to prepare
;;; for n-d castles, but it's a little over-generalized and none of
;;; the code following really supposes this.
(defun make-castle-level (castle-rooms level)
"Return a displaced array of a castle level."
(make-array (castle-level-dimensions castle-rooms)
:displaced-to castle-rooms
:displaced-index-offset
(calc-castle-level-offset level castle-rooms)))
(defun make-castle-levels (castle-rooms)
"Make a list of displaced arrays to the different castle floors."
(loop
for level from 0 below (castle-height castle-rooms)
collect
(make-castle-level castle-rooms level)))
(defun make-castle-map ()
"Make an array for storing castle map data."
(make-array *zot-castle-dimensions*
:element-type 'string
:initial-element (icon-of-unmapped)))
(defun add-castle-vectors (castle-rooms &rest vectors)
"Vectors in Zot's castle must add with modulus of array-dimensions."
(apply #'map-manifold-vectors #'+ castle-rooms vectors))
(defun subtract-castle-vectors (castle-rooms &rest vectors)
"Vectors in Zot's castle must subtract with modulus of array-dimensions."
(map-manifold-vectors #'- castle-rooms vectors))
;;;; Room and creature types
(defparameter *rooms*
'(pool chest gold-pieces flares warp sinkhole crystal-orb book)
"List of the room creature types in the castle.")
(defun room-p (creature)
"Is this creature a room?"
(find creature *rooms*))
(defparameter *monsters*
'(kobold orc wolf goblin ogre troll bear
minotaur gargoyle chimera balrog dragon)
"List of the monster creature types in the castle.")
(defun monster-p (maybe)
(find maybe *monsters*))
(deftype monster ()
`(satisfies monster-p))
(defun random-monster ()
"Return a random monster."
(random-elt *monsters*))
;; (defun adversary-p (creature)
;; "Is this creature an adversary?"
;; (assert (creature-p creature))
;; (find creature (append *monsters* '(vendor)))
;; ;; FIXME: The castle can tell us if the vendor is an enemy or
;; ;; not. This may just represent whether the creature is attackable.
;; )
;; (defun text-of-adversary (adversary)
;; "Return the enemy text."
;; (assert (adversary-p adversary))
;; (text-of-creature adversary))
;;1790 a=peek(fnd(z))-12:wc=0:if (a<13)or (vf=1)then2300
;;2300 q1=1+int(a/2):q2=a+2:q3=1
(defun calc-adversary-value (adversary)
;; FIXME: creature-battle-skill, creature-combat value?
"Return the enemy value."
;; (assert (adversary-p adversary))
(+ -12 (value-of-creature adversary)))
(defun calc-adversary-hit-points (adversary)
"Return the enemy hit points."
;; (assert (adversary-p adversary))
(+ 2 (calc-adversary-value adversary)))
(defun calc-adversary-strike-damage (adversary)
"Return the enemy hit points."
;; (assert (adversary-p adversary))
(1+ (floor (calc-adversary-value adversary) 2)))
;;; TODO: vendors can have hp and dmg too
(defparameter *treasures*
'(ruby-red norn-stone pale-pearl opal-eye
green-gem blue-flame palantir silmaril)
"List of the treasures in the castle.")
(defun treasure-p (maybe)
(find maybe *treasures*))
(deftype treasure ()
`(satisfies treasure-p))
(defun random-treasure ()
"Return a random treasure."
(random-elt *treasures*))
(defun value-of-treasure (treasure)
"Return treasure index number."
(declare (type treasure treasure))
(position treasure *treasures*))
(defun sort-treasure-list (treasure-list)
"Sort the given treasure list."
(sort treasure-list
#'<
:key #'value-of-treasure))
(defun type-of-creature (creature)
"Return the type of creature given."
;; FIXME: convert to typecase
(typecase creature
(monster 'monster)
(treasure 'treasure)
(otherwise creature)))
(defparameter *meals*
'("wich" " stew" " soup" " burger"
" roast" " munchy" " taco" " pie")
"Names of the eight recipes (orc tacos, etc)")
(defun random-meal ()
"Return a random eat."
(random-elt *meals*))
(defparameter *entrance* '(0 0 3)
"Coordinates of the entrance")
;;;; Adventurer attributes