-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphalanx.lisp
2273 lines (2070 loc) · 91.3 KB
/
phalanx.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
;;;; Phalanx
;;; A tactical 7DRL about a Roman legionary who has his soul split in two by a witch
;;; Copyright (C) 2018 Keith Bateman
;;; Email: [email protected]
;;; 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/.
(in-package :cl-user)
(defpackage :phalanx
(:use :cl :curses :md5))
(in-package :phalanx)
;;; Macros and Helper Functions
(defmacro do-while (test &rest body)
`(progn ,@body
(do ()
((not ,test))
,@body)))
(defmacro while (test &rest body)
`(do ()
((not ,test))
,@body))
;; var starts at start and is incremented by step every time until it equals end. Note that if dostep is given an incorrect step it can easily form an infinite loop, as there is no check for this.
(defmacro dostep ((var start end &optional (step 1)) &rest body)
(let ((v (gensym)))
(setq v var)
`(do ((,v ,start (+ ,v ,step)))
((= ,v ,end) (progn ,@body nil))
,@body)))
(defmacro format-message ((str &rest format-args) &key (color :cwhite))
`(message (format nil ,str ,@format-args) :color ,color))
;; Helper macro for init-print. Returns the slot-value if the slot is bound or the symbol unbound if the slot is not bound
(defmacro slot-value-or-unbound (instance slot)
`(if (slot-boundp ,instance ,slot)
(slot-value ,instance ,slot)
'unbound))
;; Create a printer for the specified class (unquoted) with the specified slots (quoted)
(defmacro init-print (class slots)
`(defmethod print-object ((instance ,class) stream)
(let ((desc (cons ',class (mapcar (lambda (slot) (cons slot (slot-value-or-unbound instance slot))) ,slots))))
(write desc :stream stream))))
(defun random-list (lst)
"Takes a random element from a list using equal weight"
(nth (random (length lst)) lst))
(defun random-list-weighted (lst)
"Takes a list where each element is a cons of a probability (out of 100) and an element and takes a random element from that list"
(let ((num (random 100))
(weights 0)
(retval nil))
(mapcar (lambda (elem) (if (< num (+ weights (car elem)))
(progn (setf retval (cdr elem))
(setf num 101))
(setf weights (+ weights (car elem)))))
lst)
retval))
(defun random-probability (&optional (percentage 50))
"Takes a percentage out of 100 and returns t or nil randomly"
(< (random 100) percentage))
(defun random-deviation (num &key (zero-p nil))
"Gives a random number from -num to +num inclusive, possibly including zero"
(let ((chosen (- (random (+ 1 (* 2 num))) num)))
(if (and (not zero-p)
(= chosen 0))
(random-deviation num)
chosen)))
(defun bound (num boundary &optional (lower-limit nil))
"Gives num if num is less than boundary, otherwise gives boundary"
(if (> num boundary)
boundary
(if (and lower-limit (< num lower-limit))
lower-limit
num)))
;;; Name generator
(defun gen-roman-name ()
(let ((praenomen (list "Gaius" "Lucius" "Marcus" "Publius" "Quintus" "Titus" "Tiberius" "Sextus" "Aulus" "Decimus" "Gnaeus" "Spurius" "Manius" "Servius" "Appius" "Numerius"))
(nomen (list "Annius" "Antonius" "Arrius" "Artorius" "Asinius" "Atilius" "Atius" "Aurelius" "Autronius" "Caecilius" "Caedicius" "Caelius" "Calidius" "Calpurnius" "Cassius" "Claudius" "Cloelius" "Cocceius" "Cominius" "Cornelius" "Coruncanius" "Curiatius" "Curius" "Curtius" "Decius" "Didius" "Domitius" "Duilius" "Durmius" "Equitius" "Fabius" "Fabricius" "Fannius" "Flavius" "Fulvius" "Furius" "Gabinius" "Galerius" "Geganius" "Gellius" "Geminius" "Genucius" "Gratius" "Herennius" "Hirtius" "Horatius" "Hortensius" "Hostilius" "Iulius" "Iunius" "Iuventius" "Laelius" "Lartius" "Licinius" "Livius" "Lucilius" "Lucretius" "Manlius" "Marcius" "Marius" "Memmius" "Menenius" "Minicius" "Minius" "Minucius" "Modius" "Mucius" "Naevius" "Nautius" "Numerius" "Numicius" "Octavius" "Ovidius" "Papirius" "Petronius" "Pinarius" "Pompeius" "Pompilius" "Pontius" "Popillius" "Porcius" "Postumius" "Quinctilius" "Quinctius" "Rubellius" "Rufius" "Rutilius" "Sallustius" "Salonius" "Salvius" "Scribonius" "Seius" "Sempronius" "Sentius" "Sergius" "Sertorius" "Servilius" "Sextius" "Sicinius" "Suetonius" "Sulpicius" "Tarpeius" "Tarquitius" "Terentius" "Titinius" "Titurius" "Tuccius" "Tullius" "Ulpius" "Valerius" "Vedius" "Velleius" "Vergilius" "Verginius" "Vibius" "Villius" "Vipsanius" "Vitellius" "Vitruvius" "Volumnius"))
(cognomen (list "Aculeo" "Agricola" "Agrippa" "Ahala" "Ahenobarbus" "Albinus" "Albus" "Ambustus" "Annalis" "Aquila" "Aquilinus" "Arvina" "Asellio" "Asina" "Atellus" "Avitus" "Balbus" "Barba" "Barbatus" "Bassus" "Bestia" "Bibaculus" "Bibulus" "Blaesus" "Brocchus" "Brutus" "Bubulcus" "Bucco" "Bulbus" "Buteo" "Caecus" "Caepio" "Caesar" "Calidus" "Calvinus" "Calvus" "Camillus" "Caninus" "Canus" "Capito" "Carbo" "Catilina" "Cato" "Catulus" "Celer" "Celsus" "Cethegus" "Cicero" "Cicurinus" "Cilo" "Cincinnatus" "Cinna" "Cordus" "Cornicen" "Cornutus" "Corvinus" "Corvus" "Cossus" "Costa" "Cotta" "Crassipes" "Crassus" "Crispinus" "Crispus" "Culleo" "Curio" "Cursor" "Curvus" "Denter" "Dento" "Dives" "Dolabella" "Dorsuo" "Drusus" "Figulus" "Fimbria" "Flaccus" "Flavus" "Florus" "Fronto" "Fullo" "Fusus" "Galeo" "Gemellus" "Glabrio" "Gracchus" "Gurges" "Habitus" "Helva" "Imperiosus" "Iullus" "Labeo" "Lactuca" "Laenas" "Lanatus" "Laevinus" "Laterensis" "Lentulus" "Lepidus" "Libo" "Licinus" "Longus" "Lucullus" "Lupus" "Lurco" "Macer" "Macula" "Malleolus" "Mamercus" "Marcellus" "Maro" "Merenda" "Mergus" "Merula" "Messalla" "Metellus" "Murena" "Mus" "Musca" "Nasica" "Naso" "Natta" "Nepos" "Nero" "Nerva" "Niger" "Novellus" "Ocella" "Pacilus" "Paetus" "Pansa" "Papus" "Paterculus" "Paullus" "Pavo" "Pera" "Pictor" "Piso" "Plancus" "Plautus" "Poplicola" "Postumus" "Potitus" "Praeconinus" "Praetextatus" "Priscus" "Proculus" "Publicola" "Pulcher" "Pullus" "Pulvillus" "Purpureo" "Quadratus" "Ralla" "Regillus" "Regulus" "Rufus" "Ruga" "Rullus" "Rutilus" "Salinator" "Saturninus" "Scaeva" "Scaevola" "Scapula" "Scaurus" "Scipio" "Scrofa" "Seneca" "Severus" "Silanus" "Silo" "Silus" "Stolo" "Strabo" "Structus" "Sulla" "Sura" "Taurus" "Triarius" "Trigeminus" "Trio" "Tubero" "Tubertus" "Tubulus" "Tuditanus" "Tullus" "Turdus" "Varro" "Varus" "Vatia" "Verres" "Vespillo" "Vetus" "Vitulus" "Volusus")))
(concatenate 'string (random-list praenomen) " " (random-list nomen) " " (random-list cognomen))))
;;; Classes, Methods, and Relevant Functions
(defclass tile ()
((blocks :initarg :blocks :accessor blocks-p)
(blocks-sight :initform t :initarg :blocks-sight :accessor blocks-sight-p)
(lit :initform nil :accessor lit)
(explored :initform nil :accessor explored-p)))
(init-print tile '(blocks blocks-sight lit explored))
(defclass obj ()
((x :initarg :x :accessor get-x)
(y :initarg :y :accessor get-y)
(char :initarg :char :accessor get-char)
(name :initarg :name :accessor get-name)
(color :initform :cwhite :initarg :color)
(blocks :initform nil :initarg :blocks :accessor blocks-p)
(blocks-sight :initform nil :initarg :blocks-sight :accessor blocks-sight-p)
(pickup :initform nil :initarg :pickup :accessor pickup-p)))
(init-print obj '(x y char name color blocks blocks-sight pickup))
(defclass item (obj)
((pickup :initform t)
(use :initform '(lambda (i p hp) t) :initarg :use :accessor get-use)
(one-use :initform nil :initarg :one-use :accessor one-use-p)
(takes-turn :initform nil :initarg :takes-turn :accessor takes-turn-p)
(atk-bonus :initform 0 :initarg :atk-bonus :accessor get-atk-bonus)
(def-bonus :initform 0 :initarg :def-bonus :accessor get-def-bonus)
(max-hp-bonus :initform 0 :initarg :max-hp-bonus :accessor get-max-hp-bonus)))
(init-print item '(x y char name color blocks blocks-sight pickup use one-use takes-turn atk-bonus def-bonus max-hp-bonus))
(defclass monster (obj)
((hp :initarg :hp :accessor get-hp)
(max-hp :accessor get-max-hp)
(attack :initarg :atk :accessor get-atk)
(defense :initarg :def :accessor get-def)
(ai :initform 'basic-ai :initarg :ai :accessor get-ai)
(death :initform 'basic-death :initarg :death :accessor get-death)))
(defmethod initialize-instance :after ((mon monster) &key)
(when (slot-boundp mon 'hp)
(setf (get-max-hp mon) (get-hp mon))))
(defmethod take-turn ((mon monster))
(funcall (coerce (get-ai mon) 'function) mon))
(defmethod kill-monster ((mon monster))
(funcall (coerce (get-death mon) 'function) mon))
(defmethod basic-ai ((mon monster))
(cond ((not (or (can-see-p mon (get-p1 *player*))
(can-see-p mon (get-p2 *player*))))
;; Wander
(move-mon mon (- (random 3) 1) (- (random 3) 1)) ; Something like this, but really we'd prefer to have the monster explore the dungeon and remember where things are (like the player)
)
((<= (get-hp mon) (/ (max-hp mon) 5))
(let ((map (dijkstra-map (cons (get-x (get-p1 *player*)) (get-y (get-p1 *player*)))
(cons (get-x (get-p2 *player*)) (get-y (get-p2 *player*))))))
(dotimes (x (- *screen-width* 1))
(dotimes (y (- *screen-height* 1))
(when (aref map x y)
(setf (aref map x y) (* (aref map x y) -1.2)))))
(let ((path (dijkstra-path map (cons (get-x mon) (get-y mon)))))
(move-mon mon (- (car path) (get-x mon)) (- (cdr path) (get-y mon)))))
;; PANIC. Run away
t)
((and (> (length (points (make-instance 'line :x1 (get-x mon) :y1 (get-y mon)
:x2 (get-x (get-p1 *player*)) :y2 (get-y (get-p1 *player*))))) 2)
(> (length (points (make-instance 'line :x1 (get-x mon) :y1 (get-y mon)
:x2 (get-x (get-p2 *player*)) :y2 (get-y (get-p2 *player*))))) 2)
(not (null (dijkstra-path (apply #'dijkstra-map (append (list (cons (cons (get-x (get-p1 *player*)) (get-y (get-p1 *player*)))
(cons (get-x mon) (get-y mon)))
(cons (cons (get-x (get-p2 *player*)) (get-y (get-p2 *player*)))
(cons (get-x mon) (get-y mon))))
(mapcar (lambda (g) (cons g (cons (get-x mon) (get-y mon)))) *goals*)))
(cons (get-x mon) (get-y mon))))))
(let ((path (dijkstra-path (apply #'dijkstra-map (append (list (cons (cons (get-x (get-p1 *player*)) (get-y (get-p1 *player*)))
(cons (get-x mon) (get-y mon)))
(cons (cons (get-x (get-p2 *player*)) (get-y (get-p2 *player*)))
(cons (get-x mon) (get-y mon))))
(mapcar (lambda (g) (cons g (cons (get-x mon) (get-y mon)))) *goals*)))
(cons (get-x mon) (get-y mon)))))
(move-mon mon (- (car path) (get-x mon)) (- (cdr path) (get-y mon)))
;; Chase
t))
(t
(attack mon *player*) ; Attack
)))
(defmethod basic-death ((mon monster))
(setf (get-char mon) #\%)
(setf (get-ai mon) '(lambda (mon) t))
(setf (blocks-p mon) nil)
(format-message ("~A dies a horrible and painful death" (get-name mon)) :color :cred)
(setf (get-exp *player*) (+ (get-exp *player*) (cond ((> (get-dlvl *player*) 6)
50)
((> (get-dlvl *player*) 3)
30)
(t
10))))
(when (> (get-exp *player*) (threshold *player*))
(level-up *player*)))
(defmethod player-death ((mon monster))
(setf (get-char mon) #\%)
(setf *game-state* nil)
(message "You die a lonely and depressing death" :color :cred)
(render-messages)
(delete-file "phalanx-save.sav")
(getch))
(defmethod leader-death ((mon monster))
;; NOTE: this removes all goals associated with the leader, even if they're associated with another leader
(setf *goals* (remove-if (lambda (x) (member x (get-goals mon)))
*goals*))
(setf (get-char mon) #\%)
(setf (get-ai mon) '(lambda (mon) t))
(setf (blocks-p mon) nil)
(format-message ("~A dies a noble death" (get-name mon)) :color :cred)
(setf (get-exp *player*) (+ (get-exp *player*) 100))
(when (> (get-exp *player*) (threshold *player*))
(level-up *player*)))
(defmethod witch-death ((mon monster))
(setf *goals* (remove-if (lambda (x) (member x (get-goals mon)))
*goals*))
(setf (get-char mon) #\%)
(setf (get-ai mon) '(lambda (mon) t))
(setf (blocks-p mon) nil)
(story 'win)
(setf *game-state* nil))
(defmethod can-see-p ((mon-looking monster) (mon-looked-at monster))
(member (cons (get-x mon-looked-at) (get-y mon-looked-at))
(get-sight-line (make-instance 'line :x1 (get-x mon-looking) :y1 (get-y mon-looking)
:x2 (get-x mon-looked-at) :y2 (get-y mon-looked-at)))
:test #'equal))
(defmethod atk ((mon monster))
(defun bonus-atk (mon)
(if (typep mon 'player)
(+ (if (get-inv (get-p1 mon))
(reduce #'max (mapcar #'get-atk-bonus (get-inv (get-p1 mon))))
0)
(if (get-inv (get-p2 mon))
(reduce #'max (mapcar #'get-atk-bonus (get-inv (get-p2 mon))))
0))
0))
(+ (get-atk mon) (bonus-atk mon)))
(defmethod def ((mon monster))
(defun bonus-def (mon)
(if (typep mon 'player)
(+ (if (get-inv (get-p1 mon))
(reduce #'max (mapcar #'get-def-bonus (get-inv (get-p1 mon))))
0)
(if (get-inv (get-p2 mon))
(reduce #'max (mapcar #'get-def-bonus (get-inv (get-p2 mon))))
0))
0))
(+ (get-def mon) (bonus-def mon)))
(defmethod max-hp ((mon monster))
(defun bonus-max-hp (mon)
(if (typep mon 'player)
(+ (if (get-inv (get-p1 mon))
(reduce #'max (mapcar #'get-max-hp-bonus (get-inv (get-p1 mon))))
0)
(if (get-inv (get-p2 mon))
(reduce #'max (mapcar #'get-max-hp-bonus (get-inv (get-p2 mon))))
0))
0))
(+ (get-max-hp mon) (bonus-max-hp mon)))
(defmethod deal-damage ((mon monster) dam)
(setf (get-hp mon) (- (get-hp mon) dam))
(if (<= (get-hp mon) 0)
(kill-monster mon)))
(defmethod attack ((attacker monster) (defender monster) &optional (hit-message "~A attacks ~A for ~A damage!") (miss-message "~A lunges at ~A and misses!"))
"Have the attacker monster perform an attack on the defender monster"
(let ((hit-p (< (random 1.0) (/ (atk attacker) (+ (atk attacker) (def defender)))))
(damage (+ (random 3) (round (/ (atk attacker) (def defender))))))
(cond (hit-p (format-message (hit-message (get-name attacker) (get-name defender) damage) :color :cred)
(deal-damage defender damage))
(t (format-message (miss-message (get-name attacker) (get-name defender)) :color :cred)))))
(defmethod move-mon ((mon monster) dx dy)
(let ((new-x (+ (get-x mon) dx))
(new-y (+ (get-y mon) dy)))
(when (not (blocked-p new-x new-y))
(setf (get-x mon) new-x)
(setf (get-y mon) new-y))))
(init-print monster '(x y char name color blocks blocks-sight pickup hp max-hp attack defense ai death))
(defclass leader (monster)
((goals :initform nil :initarg :goals :accessor get-goals)
(ai :initform 'self-leader-ai :initarg :ai :accessor get-ai)
(death :initform 'leader-death :initarg :death :accessor get-death)))
(init-print leader '(x y char name color blocks blocks-sight pickup hp max-hp attack defense ai death goals))
(defmethod add-goal ((l leader) goal)
(push goal (get-goals l))
(if (member goal *goals* :test #'equal)
goals
(push goal *goals*)))
(defmethod self-leader-ai ((l leader))
(setf *goals* (remove-if (lambda (x) (member x (get-goals l)))
*goals*))
(basic-ai l) ; Basic AI is used to give easy run-away functionality
(setf (get-goals l) nil) ; Reset goals for better or worse
(add-goal l (cons (get-x l) (get-y l))))
(defmethod witch-ai ((l leader))
(setf *goals* (remove-if (lambda (x) (member x (get-goals l)))
*goals*))
(cond ((or (random-probability 20)
(= (length (points (make-instance 'line :x1 (get-x l) :y1 (get-y l)
:x2 (get-x (get-p1 *player*)) :y2 (get-y (get-p1 *player*))))) 2)
(= (length (points (make-instance 'line :x1 (get-x l) :y1 (get-y l)
:x2 (get-x (get-p2 *player*)) :y2 (get-y (get-p2 *player*))))) 2))
;; Run away
(let ((map (dijkstra-map (cons (get-x (get-p1 *player*)) (get-y (get-p1 *player*)))
(cons (get-x (get-p2 *player*)) (get-y (get-p2 *player*))))))
(dotimes (x (- *screen-width* 1))
(dotimes (y (- *screen-height* 1))
(when (aref map x y)
(setf (aref map x y) (* (aref map x y) -1.2)))))
(let ((path (dijkstra-path map (cons (get-x l) (get-y l)))))
(move-mon l (- (car path) (get-x l)) (- (cdr path) (get-y l))))))
((or (and (can-see-p l (get-p1 *player*))
(< (length (points (make-instance 'line :x1 (get-x l) :y1 (get-y l)
:x2 (get-x (get-p1 *player*)) :y2 (get-y (get-p1 *player*)))))
6))
(and (can-see-p l (get-p2 *player*))
(< (length (points (make-instance 'line :x1 (get-x l) :y1 (get-y l)
:x2 (get-x (get-p2 *player*)) :y2 (get-y (get-p2 *player*)))))
6)))
;; Attack
(defun spawn-specter ()
(let ((spawn-points (remove-if-not (lambda (pt) (not (blocked-p (car pt) (cdr pt))))
(cdr (butlast (points (make-instance 'line :x1 (get-x l) :y1 (get-y l)
:x2 (get-x (get-p2 *player*)) :y2 (get-y (get-p2 *player*))))
1)))))
(if (not (null spawn-points))
(push (make-instance 'monster :x (caar spawn-points) :y (cdar spawn-points)
:hp (+ 100 (random-deviation 5 :zero-p t))
:atk (+ 30 (random-deviation 2 :zero-p t))
:def (+ 30 (random-deviation 2 :zero-p t))
:name "Specter" :char #\s :color :cyellow :blocks t)
*objects*)
(message "The Witch gestures wildly"))))
(eval (random-list-weighted (list (cons 40 `(message "The Witch gestures wildly"))
(cons 30 `(spawn-specter))
(cons 30 `(progn (attack ,l ,*player* "~A zaps ~A with lightning for ~A damage" "~A zaps lightning which shoots past ~A"))))))))
(setf (get-goals l) nil) ; Reset goals for better or worse
(add-goal l (cons (get-x l) (get-y l))))
(defclass half-player (monster)
((inventory :initform nil :initarg :inv :accessor get-inv)))
(init-print half-player '(x y color blocks blocks-sight pickup inventory))
(defgeneric pickup (monster)
(:documentation "Pick up the item where the monster is standing"))
(defmethod pickup ((p half-player))
(if (< (length (get-inv p)) *inventory-size*)
(let ((objs (remove-if-not (lambda (obj) (and (= (get-x obj) (get-x p))
(= (get-y obj) (get-y p))
(pickup-p obj)))
*objects*)))
(when (not (null (car objs)))
(push (car objs) (get-inv p))
(setf *objects* (delete (car objs) *objects* :count 1)))
(car objs))
(progn (message "Inventory full")
nil)))
(defmethod drop-item ((i item) (p half-player))
(setf (get-x i) (get-x p))
(setf (get-y i) (get-y p))
(push i *objects*)
(setf (get-inv p) (delete i (get-inv p) :count 1)))
(defmethod power-strike ((p half-player) direction damage &key (hit-message "~A gets hit") (hit-message-color :cwhite))
"A super-powered attack"
(mapcar (lambda (mon)
(deal-damage mon damage)
(format-message (hit-message (get-name mon)) :color hit-message-color))
(remove-if-not (lambda (obj) (and (typep obj 'monster)
(not (char= (get-char obj) #\%))
(= (get-x obj) (+ (get-x p) (car direction)))
(= (get-y obj) (+ (get-y p) (cdr direction)))))
*objects*)))
(defmethod throw-item ((p half-player) direction distance damage &key (hit-message "~A gets hit") (hit-message-color :cwhite) to-drop)
(let ((path (points (make-instance 'line
:x1 (get-x p) :y1 (get-y p)
:x2 (bound (+ (get-x p) (* (car direction) distance)) (- *screen-width* 1) 1) :y2 (bound (+ (get-y p) (* (cdr direction) distance)) (- *screen-height* 1) 1))))
hit)
(dolist (pt path)
(move (cdr pt) (car pt))
(when (blocks-p (aref *map* (car pt) (cdr pt)))
(message "Hit a wall" :color hit-message-color)
(return))
(dolist (mon (remove-if-not (lambda (obj) (and (typep obj 'monster)
(not (char= (get-char obj) #\%)))) *objects*))
(when (and (= (get-x mon) (car pt))
(= (get-y mon) (cdr pt)))
(deal-damage mon damage)
(format-message (hit-message (get-name mon)) :color hit-message-color)
(setf hit t)))
(when hit
(when to-drop
(setf (get-x to-drop) (car pt))
(setf (get-y to-drop) (cdr pt))
(push to-drop *objects*))
(return)))
(when (and (not hit) to-drop)
(setf (get-x to-drop) (bound (+ (get-x p) (* (car direction) distance)) (- *screen-width* 1) 1))
(setf (get-y to-drop) (bound (+ (get-y p) (* (cdr direction) distance)) (- *screen-height* 1) 1))
(push to-drop *objects*))))
(defmethod check-for-mon-attack ((p half-player) dx dy)
"Attacks space where player would have moved to if possible"
(let ((new-x (+ (get-x p) dx))
(new-y (+ (get-y p) dy)))
(dolist (defender (remove-if-not (lambda (obj) (typep obj 'monster)) *objects*))
(when (and (= (get-x defender) new-x)
(= (get-y defender) new-y))
(attack *player* defender))))
t)
(defclass player (monster)
((p1 :initform (make-instance 'half-player) :initarg :p1 :accessor get-p1)
(p2 :initform (make-instance 'half-player) :initarg :p2 :accessor get-p2)
(full-name :initform (gen-roman-name) :accessor get-full-name)
(dlvl :initform 1 :accessor get-dlvl)
(lvl :initform 1 :accessor get-lvl)
(exp :initform 0 :accessor get-exp)))
(defmethod initialize-instance :after ((p player) &key)
(setf (get-name p) (subseq (get-full-name p) 0 (position #\Space (get-full-name p)))))
(init-print player '(p1 p2 name lvl dlvl exp x y hp max-hp attack defense ai death))
(defmethod use-item ((i item) (p player) (hp half-player))
(funcall (coerce (get-use i) 'function) i p hp))
(defmethod threshold ((p player))
(defun to-next-level (l)
(if (<= l 1)
50
(* (+ 1 (expt 0.85 l)) (to-next-level (- l 1)))))
(defun total-exp (l)
(if (<= l 1)
(to-next-level l)
(+ (to-next-level l) (total-exp (- l 1)))))
(total-exp (get-lvl p)))
(defmethod level-up ((p player))
(setf (get-lvl p) (+ (get-lvl p) 1))
(setf (get-atk p) (+ (get-atk p) (* (get-lvl p) (+ 1 (random 3)))))
(setf (get-def p) (+ (get-def p) (* (get-lvl p) (+ 1 (random 3)))))
(setf (get-max-hp p) (+ (get-max-hp p) (* (get-lvl p) (+ 1 (random 3)))))
(setf (get-hp p) (max-hp p))
(message "Level up!" :color :cblue)
(stats)
(refresh))
(defmethod down ((p player))
;; Currently dungeon generation is Angband-style, mostly because of space considerations, but also due to laziness
(dolist (first-stair *objects*)
(when (and (string= (get-name first-stair) "downstair")
(= (get-x first-stair) (get-x (get-p1 *player*)))
(= (get-y first-stair) (get-y (get-p1 *player*))))
(dolist (second-stair (delete first-stair *objects*))
(when (and (string= (get-name second-stair) "downstair")
(= (get-x second-stair) (get-x (get-p2 *player*)))
(= (get-y second-stair) (get-y (get-p2 *player*))))
(setf *objects* nil)
(setf (get-dlvl p) (+ (get-dlvl p) 1))
(erase)
(init-map)
(render-all))))))
(defmethod up ((p player))
(dolist (first-stair *objects*)
(when (and (string= (get-name first-stair) "upstair")
(= (get-x first-stair) (get-x (get-p1 *player*)))
(= (get-y first-stair) (get-y (get-p1 *player*))))
(dolist (second-stair (remove first-stair *objects*))
(when (and (string= (get-name second-stair) "upstair")
(= (get-x second-stair) (get-x (get-p2 *player*)))
(= (get-y second-stair) (get-y (get-p2 *player*))))
(when (or (not (= (get-dlvl p) 1))
(warn-of-impending-doom))
(setf *objects* nil)
(setf (get-dlvl p) (- (get-dlvl p) 1))
(init-map :stair 'down))
(erase)
(render-all))))))
(defmethod pickup ((p player))
(let ((pickup-1 (pickup (get-p1 p)))
(pickup-2 (pickup (get-p2 p))))
(when pickup-1
(format-message ("~A picked up ~A" (get-name p) (get-name pickup-1)) :color :cgreen))
(when pickup-2
(format-message ("~A picked up ~A" (get-name p) (get-name pickup-2)) :color :cgreen))))
(defun move-player (dx dy)
"Moves the player if possible"
(cond ((and (not (blocked-p (+ (get-x (get-p1 *player*)) dx) (+ (get-y (get-p1 *player*)) dy)))
(not (blocked-p (+ (get-x (get-p2 *player*)) dx) (+ (get-y (get-p2 *player*)) dy))))
(move-mon (get-p1 *player*) dx dy)
(move-mon (get-p2 *player*) dx dy)
(fov-calculate))
(t
(check-for-mon-attack (get-p1 *player*) dx dy)
(check-for-mon-attack (get-p2 *player*) dx dy))))
(defun separate-player ()
"Moves the player units farther apart if possible"
(defun mon-away-direction (mon pt)
(let* ((x (car pt))
(y (cdr pt))
(dist (sqrt (+ (expt (- x (get-x mon)) 2) (expt (- y (get-y mon)) 2))))
(dx (round (/ (- x (get-x mon))
dist)))
(dy (round (/ (- y (get-y mon))
dist))))
(cons (- dx) (- dy))))
(let* ((ln (make-instance 'line
:x1 (get-x (get-p1 *player*)) :y1 (get-y (get-p1 *player*))
:x2 (get-x (get-p2 *player*)) :y2 (get-y (get-p2 *player*))))
(p1-dir (mon-away-direction (get-p1 *player*) (center ln)))
(p2-dir (mon-away-direction (get-p2 *player*) (center ln))))
(cond ((and (not (blocked-p (+ (get-x (get-p1 *player*)) (car p1-dir)) (+ (get-y (get-p1 *player*)) (cdr p1-dir))))
(not (blocked-p (+ (get-x (get-p2 *player*)) (car p2-dir)) (+ (get-y (get-p2 *player*)) (cdr p2-dir)))))
(move-mon (get-p1 *player*) (car p1-dir) (cdr p1-dir))
(move-mon (get-p2 *player*) (car p2-dir) (cdr p2-dir))
(fov-calculate))
(t
(check-for-mon-attack (get-p1 *player*) (car p1-dir) (cdr p1-dir))
(check-for-mon-attack (get-p2 *player*) (car p2-dir) (cdr p2-dir))))))
(defun gather-player ()
"Moves the player units closer together if possible"
(defun mon-towards-direction (mon pt)
(let* ((x (car pt))
(y (cdr pt))
(dist (sqrt (+ (expt (- x (get-x mon)) 2) (expt (- y (get-y mon)) 2))))
(dx (round (/ (- x (get-x mon))
dist)))
(dy (round (/ (- y (get-y mon))
dist))))
(cons dx dy)))
(let* ((ln (make-instance 'line
:x1 (get-x (get-p1 *player*)) :y1 (get-y (get-p1 *player*))
:x2 (get-x (get-p2 *player*)) :y2 (get-y (get-p2 *player*))))
(p1-dir (mon-towards-direction (get-p1 *player*) (center ln)))
(p2-dir (mon-towards-direction (get-p2 *player*) (center ln))))
(cond ((and (not (blocked-p (+ (get-x (get-p1 *player*)) (car p1-dir)) (+ (get-y (get-p1 *player*)) (cdr p1-dir))))
(not (blocked-p (+ (get-x (get-p2 *player*)) (car p2-dir)) (+ (get-y (get-p2 *player*)) (cdr p2-dir))))
(or (not (= (+ (get-x (get-p1 *player*)) (car p1-dir)) ; For now I just check to avoid moving to the center, but eventually I would like to treat it as two attacks on the center point
(+ (get-x (get-p2 *player*)) (car p2-dir))))
(not (= (+ (get-y (get-p1 *player*)) (cdr p1-dir))
(+ (get-y (get-p2 *player*)) (cdr p2-dir))))))
(move-mon (get-p1 *player*) (car p1-dir) (cdr p1-dir))
(move-mon (get-p2 *player*) (car p2-dir) (cdr p2-dir))
(fov-calculate))
(t
(check-for-mon-attack (get-p1 *player*) (car p1-dir) (cdr p1-dir))
(check-for-mon-attack (get-p2 *player*) (car p2-dir) (cdr p2-dir))))))
(defun rotate-player (direction)
"Rotate player in a clockwise or counter direction"
(defun find-rect (center pt size)
"Brute force iterative method for finding a rectangle with specified center containing edge point pt"
(if (not (member pt
(wall-points (make-instance 'rect
:x (- (car center) size) :y (- (cdr center) size)
:width (+ 1 (* 2 size)) :height (+ 1 (* 2 size))))
:test #'equal))
(find-rect center pt (+ size 1))
(make-instance 'rect
:x (- (car center) size) :y (- (cdr center) size)
:width (+ 1 (* 2 size)) :height (+ 1 (* 2 size)))))
(let ((ln (make-instance 'line
:x1 (get-x (get-p1 *player*)) :y1 (get-y (get-p1 *player*))
:x2 (get-x (get-p2 *player*)) :y2 (get-y (get-p2 *player*)))))
;; FIXME: This is a mess because I got rid of the abstraction in order to check for blockage "in parallel"
(cond ((eq direction 'clockwise)
(let ((p1-dir (let* ((pts (reverse (wall-points (find-rect (center ln) (cons (get-x (get-p1 *player*)) (get-y (get-p1 *player*))) 1))))
(subsequent (member (cons (get-x (get-p1 *player*)) (get-y (get-p1 *player*)))
pts :test #'equal)))
(if (> (length subsequent) 1)
(let ((goto (cadr subsequent)))
(cons (- (car goto) (get-x (get-p1 *player*)))
(- (cdr goto) (get-y (get-p1 *player*)))))
(let ((goto (car pts)))
(cons (- (car goto) (get-x (get-p1 *player*)))
(- (cdr goto) (get-y (get-p1 *player*))))))))
(p2-dir (let* ((pts (reverse (wall-points (find-rect (center ln) (cons (get-x (get-p2 *player*)) (get-y (get-p2 *player*))) 1))))
(subsequent (member (cons (get-x (get-p2 *player*)) (get-y (get-p2 *player*)))
pts :test #'equal)))
(if (> (length subsequent) 1)
(let ((goto (cadr subsequent)))
(cons (- (car goto) (get-x (get-p2 *player*)))
(- (cdr goto) (get-y (get-p2 *player*)))))
(let ((goto (car pts)))
(cons (- (car goto) (get-x (get-p2 *player*)))
(- (cdr goto) (get-y (get-p2 *player*)))))))))
(cond ((and (not (blocked-p (+ (get-x (get-p1 *player*)) (car p1-dir)) (+ (get-y (get-p1 *player*)) (cdr p1-dir))))
(not (blocked-p (+ (get-x (get-p2 *player*)) (car p2-dir)) (+ (get-y (get-p2 *player*)) (cdr p2-dir)))))
(move-mon (get-p1 *player*) (car p1-dir) (cdr p1-dir))
(move-mon (get-p2 *player*) (car p2-dir) (cdr p2-dir))
(fov-calculate))
(t
(check-for-mon-attack (get-p1 *player*) (car p1-dir) (cdr p1-dir))
(check-for-mon-attack (get-p2 *player*) (car p2-dir) (cdr p2-dir))))))
((eq direction 'counter)
(let ((p1-dir (let* ((pts (wall-points (find-rect (center ln) (cons (get-x (get-p1 *player*)) (get-y (get-p1 *player*))) 1)))
(subsequent (member (cons (get-x (get-p1 *player*)) (get-y (get-p1 *player*)))
pts :test #'equal)))
(if (> (length subsequent) 1)
(let ((goto (cadr subsequent)))
(cons (- (car goto) (get-x (get-p1 *player*)))
(- (cdr goto) (get-y (get-p1 *player*)))))
(let ((goto (car pts)))
(cons (- (car goto) (get-x (get-p1 *player*)))
(- (cdr goto) (get-y (get-p1 *player*))))))))
(p2-dir (let* ((pts (wall-points (find-rect (center ln) (cons (get-x (get-p2 *player*)) (get-y (get-p2 *player*))) 1)))
(subsequent (member (cons (get-x (get-p2 *player*)) (get-y (get-p2 *player*)))
pts :test #'equal)))
(if (> (length subsequent) 1)
(let ((goto (cadr subsequent)))
(cons (- (car goto) (get-x (get-p2 *player*)))
(- (cdr goto) (get-y (get-p2 *player*)))))
(let ((goto (car pts)))
(cons (- (car goto) (get-x (get-p2 *player*)))
(- (cdr goto) (get-y (get-p2 *player*)))))))))
(cond ((and (not (blocked-p (+ (get-x (get-p1 *player*)) (car p1-dir)) (+ (get-y (get-p1 *player*)) (cdr p1-dir))))
(not (blocked-p (+ (get-x (get-p2 *player*)) (car p2-dir)) (+ (get-y (get-p2 *player*)) (cdr p2-dir)))))
(move-mon (get-p1 *player*) (car p1-dir) (cdr p1-dir))
(move-mon (get-p2 *player*) (car p2-dir) (cdr p2-dir))
(fov-calculate))
(t
(check-for-mon-attack (get-p1 *player*) (car p1-dir) (cdr p1-dir))
(check-for-mon-attack (get-p2 *player*) (car p2-dir) (cdr p2-dir))))))
(t
(error "Improper direction ~A" direction)))))
(defclass shape ()
((pts :initform nil :accessor points)))
(init-print shape '(pts))
(defgeneric draw (shape)
(:documentation "Draw the given shape on the screen"))
(defgeneric center (shape)
(:documentation "Returns the center of the given shape as a cons pair"))
(defgeneric random-point-in (shape)
(:documentation "Returns a random point inside of the given shape"))
(defmethod random-point-in ((sh shape))
(let ((pts (points sh)))
(nth (random (length pts)) pts)))
(defmethod draw ((sh shape))
(dolist (pt (points sh) sh)
(setf (slot-value (aref *map* (car pt) (cdr pt)) 'blocks) nil)
(setf (slot-value (aref *map* (car pt) (cdr pt)) 'blocks-sight) nil)))
(defclass rect (shape)
((x1 :initarg :x :accessor get-x1)
(y1 :initarg :y :accessor get-y1)
(w :initarg :width)
(h :initarg :height)
(x2 :accessor get-x2)
(y2 :accessor get-y2)))
(defmethod initialize-instance :after ((rm rect) &key)
(let ((x1 (get-x1 rm)) (y1 (get-y1 rm))
(h (slot-value rm 'h)) (w (slot-value rm 'w)))
(setf (get-x2 rm) (+ x1 w))
(setf (get-y2 rm) (+ y1 h)))
(let ((pts nil) (x1 (get-x1 rm)) (y1 (get-y1 rm))
(x2 (get-x2 rm)) (y2 (get-y2 rm)))
(dostep (x (+ x1 1) (- x2 2))
(dostep (y (+ y1 1) (- y2 2))
(setf pts (cons (cons x y) pts))))
(setf (points rm) pts)))
(init-print rect '(x1 y1 w h x2 y2 pts))
(defgeneric wall-points (rect)
(:documentation "Returns the points of the walls of the given rectangle in clockwise order starting from the top left corner"))
(defmethod wall-points ((rm rect))
(let ((pts nil) (x1 (get-x1 rm)) (y1 (get-y1 rm))
(x2 (get-x2 rm)) (y2 (get-y2 rm)))
(dostep (x x1 (- x2 1))
(setf pts (cons (cons x y1) pts)))
(dostep (y (+ y1 1) (- y2 2))
(setf pts (cons (cons (- x2 1) y) pts)))
(dostep (x (- x2 1) x1 -1)
(setf pts (cons (cons x (- y2 1)) pts)))
(dostep (y (- y2 2) (+ y1 1) -1)
(setf pts (cons (cons x1 y) pts)))
pts))
(defclass line (shape)
((x1 :initarg :x1 :accessor get-x1)
(y1 :initarg :y1 :accessor get-y1)
(x2 :initarg :x2 :accessor get-x2)
(y2 :initarg :y2 :accessor get-y2)))
(defmethod initialize-instance :after ((ln line) &key)
;; Bresenham line drawing to get all points along the line
;; Unfortunately, this doesn't make code reuse very easy, but I tried to comment the tricky parts
(let ((x (get-x1 ln)) (y (get-y1 ln))
(x-end (get-x2 ln)) (y-end (get-y2 ln))
(dy (- (get-y2 ln) (get-y1 ln))) (dx (- (get-x2 ln) (get-x1 ln)))
x-inc y-inc to-iter (e 0))
;; Test to find which point to plot next
(defun bham-test (dx dy e)
(< (* 2 (+ e dy)) dx)) ; This uses a modification so the algorithm is only handling integer values
;; Initialization, set x-inc, y-inc, and to-iter (and maybe dx and dy for convenience) so that we can use the same code for all 8 Bresenham cases. The variable to-iter is either 'x or 'y depending on which one we're iterating over (it's the value we add to every time)
(cond ((and (<= dy dx) (>= dy 0)) ; +x +y 0 <= dy/dx <= 1
(setf x-inc 1)
(setf y-inc 1)
(setf to-iter 'x))
((and (> dy dx) (>= dx 0)) ; +x +y dy/dx > 1
(setf dy (prog2 0 dx (setf dx dy))) ; I knew HAKMEM 163 would come in handy some day. This is because we are incrementing along y, so we pretend that dx <-> dy to maximize code reuse.
(setf to-iter 'y)
(setf x-inc 1)
(setf y-inc 1))
((and (<= dy 0) (< dx 0) (<= (abs dy) (abs dx))) ; -x -y 0 <= dy/dx <= 1
(setf dx (- dx))
(setf dy (- dy))
(setf x-inc -1)
(setf y-inc -1)
(setf to-iter 'x))
((and (< dy 0) (<= dx 0) (> (abs dy) (abs dx))) ; -x -y dy/dx > 1
(setf dx (- dx))
(setf dy (- dy))
(setf x-inc -1)
(setf y-inc -1)
(setf to-iter 'y)
(setf dy (prog2 0 dx (setf dx dy))))
((and (<= (abs dy) (abs dx)) (> dx 0) (<= dy 0)) ; +x -y 0 <= abs(dy/dx) <= 1
(setf x-inc 1)
(setf y-inc -1)
(setf to-iter 'x)
(setf dy (- dy))) ; We pretend here that dy is positive to keep error calculations simple
((and (> (abs dy) (abs dx)) (>= dx 0) (< dy 0)) ; +x -y abs(dy/dx) > 1
(setf x-inc 1)
(setf y-inc -1)
(setf to-iter 'y)
(setf dy (- dy))
(setf dy (prog2 0 dx (setf dx dy))))
((and (<= (abs dy) (abs dx)) (< dx 0) (>= dy 0)) ; -x +y 0 <= abs(dy/dx) <= 1
(setf x-inc -1)
(setf y-inc 1)
(setf to-iter 'x)
(setf dx (- dx)))
((and (> (abs dy) (abs dx)) (<= dx 0) (> dy 0)) ; -x +y abs(dy/dx) > 1
(setf x-inc -1)
(setf y-inc 1)
(setf to-iter 'y)
(setf dx (- dx))
(setf dy (prog2 0 dx (setf dx dy))))
(t (error "Line does not fulfill Bresenham requirements")))
;; Perform the actual algorithm
(cond ((eq to-iter 'x)
(setf (points ln) (cons (cons x y) (points ln)))
(while (or (not (= x x-end)) (not (= y y-end)))
(bounds-check x y)
(if (bham-test dx dy e)
(progn (setf e (+ e dy))
(setf x (+ x x-inc))
(setf (points ln) (cons (cons x y) (points ln))))
(progn (setf e (- (+ e dy) dx))
(setf x (+ x x-inc))
(setf y (+ y y-inc))
(setf (points ln) (cons (cons x y) (points ln)))))))
((eq to-iter 'y)
(setf (points ln) (cons (cons x y) (points ln)))
(while (or (not (= x x-end)) (not (= y y-end)))
(bounds-check x y)
(if (bham-test dx dy e)
(progn (setf e (+ e dy))
(setf y (+ y y-inc))
(setf (points ln) (cons (cons x y) (points ln))))
(progn (setf e (- (+ e dy) dx))
(setf x (+ x x-inc))
(setf y (+ y y-inc))
(setf (points ln) (cons (cons x y) (points ln)))))))
(t (error "improper value of to-iter ~A" to-iter))))
;; Reverse the list, just so we have it in order for LoS check
(setf (points ln) (reverse (points ln))))
(init-print line '(x1 y1 x2 y2 pts))
(defmethod center ((ln line))
(cons (round (/ (+ (get-x1 ln) (get-x2 ln)) 2)) (round (/ (+ (get-y1 ln) (get-y2 ln)) 2))))
(defmethod get-sight-line ((ln line))
(let ((sight-points nil))
(dolist (pt (points ln) sight-points)
(setf sight-points (cons pt sight-points))
(when (sight-blocked-p (car pt) (cdr pt))
(return sight-points)))))
(defmethod see-along ((ln line))
(dolist (pt (get-sight-line ln))
(when (not (null pt))
(setf (lit (aref *map* (car pt) (cdr pt))) t)
(when (not (explored-p (aref *map* (car pt) (cdr pt))))
(setf (explored-p (aref *map* (car pt) (cdr pt))) t)))))
(defclass message ()
((color :initarg :color :accessor get-color)
(str :initarg :str :accessor get-string)))
(init-print message '(color str))
;;; Globals
(defconstant *screen-width* 50)
(defconstant *screen-height* 20)
(defconstant *class-list* '(tile obj monster player half-player leader item shape rect line message))
(defconstant *max-room-size* 20)
(defconstant *min-room-size* 10)
(defconstant *max-iterations* 3)
(defconstant *inventory-size* 5)
(defconstant *fov-radius* 5)
(defparameter *map* (make-array (list *screen-width* *screen-height*)))
(defparameter *objects* nil)
(defparameter *game-state* nil)
(defparameter *messages* nil)
(defparameter *goals* nil)
(defparameter *player* (make-instance 'player
:p1 (make-instance 'half-player :x 10 :y 10 :blocks-sight nil)
:p2 (make-instance 'half-player :x 12 :y 10 :blocks-sight nil)
:hp (+ 30 (random 21))
:atk (+ 5 (random 4))
:def (+ 5 (random 4))
:ai '(lambda (mon) (input))
:death 'player-death))
;;; Map Functions
(defun bounds-check (x y)
(when (or (>= x *screen-width*) (< x 0)
(>= y *screen-height*) (< y 0))
(error "Tried to access point (~A . ~A) outside of the bounds of the map" x y)))
(defun points-within (distance point)
(let (pts)
(dostep (x (if (< (- (car point) distance) 0)
0
(- (car point) distance))
(if (> (+ (car point) distance) (- *screen-width* 1))
(- *screen-width* 1)
(+ (car point) distance)))
(dostep (y (if (< (- (cdr point) distance) 0)
0
(- (cdr point) distance))
(if (> (+ (cdr point) distance) (- *screen-height* 1))
(- *screen-height* 1)
(+ (cdr point) distance)))
(setf pts (cons (cons x y) pts))))
pts))
(defun init-map (&key (stair 'up))
"Initializes the *map* global variable"
(when (= (get-dlvl *player*) 0)
(when (not (null *game-state*))
(delete-file "phalanx-save.sav"))
(setf *game-state* nil))
(dostep (x 0 (- *screen-width* 1))
(dostep (y 0 (- *screen-height* 1))
(setf (aref *map* x y) (make-instance 'tile :blocks t))))
(defun gen-pillars (&optional (num-pillars (+ 30 (random 40))))
"Adds a bunch of random pillars for variety"
(dotimes (i (+ 30 (random 40)))
(setf (aref *map*
(+ 2 (random (- *screen-width* 5)))
(+ 2 (random (- *screen-height* 5))))
(make-instance 'tile :blocks t))))
(defun big-open-level ()
"Uses 3 different dungeon generation methods in unison in an attempt to create a big open level with roughly rectangular rooms and a directional component"
(let* ((shapes (split-dungeon 0 1 1 (- *screen-width* 2) (- *screen-height* 2)))
(rects (remove-if-not (lambda (x) (typep x 'rect)) shapes))
(lines (remove-if-not (lambda (x) (typep x 'line)) shapes)))
(dolist (r rects)
(draw r))
(smooth-dungeon 1)
(dolist (l lines)
(draw l)))
(direct-dungeon :windyness (random 100) :roughness (random 50) :complexity 1 :stair stair)
;;Generate pillars
(gen-pillars))
(defun two-corridor-level ()
"Generate a level with two side-by-side corridors"
(let ((displacement (random 4)))
(direct-dungeon :windyness (random 10) :roughness (random 10) :stair nil :ystart (+ 1 displacement) :hstart 3)
(direct-dungeon :windyness (random 10) :roughness (random 10) :stair nil :ystart (- *screen-height* 6 displacement) :hstart 3)
(push (make-instance 'obj :x 1 :y (+ 2 displacement) :name "upstair" :char #\<) *objects*)
(push (make-instance 'obj :x 1 :y (- *screen-height* 6 displacement) :name "upstair" :char #\<) *objects*)
(when (eq stair 'up)
(setf (get-x (get-p1 *player*)) 1)
(setf (get-y (get-p1 *player*)) (+ 2 displacement))
(setf (get-x (get-p2 *player*)) 1)
(setf (get-y (get-p2 *player*)) (- *screen-height* 6 displacement)))
(let* ((y1 (dotimes (y *screen-height*)
(when (not (blocks-p (aref *map* (- *screen-width* 2) y)))
(return y))))
(y2 (dostep (y (- *screen-height* 2) 1 -1)
(when (and (not (blocks-p (aref *map* (- *screen-width* 2) y)))
(> (- y y1) 1)
(evenp (- y y1)))
(return y)))))
(when (eq stair 'down)
(setf (get-x (get-p1 *player*)) (- *screen-width* 2))
(setf (get-y (get-p1 *player*)) y1)
(setf (get-x (get-p2 *player*)) (- *screen-width* 2))
(setf (get-y (get-p2 *player*)) y2))
(push (make-instance 'obj
:x (- *screen-width* 2) :y y1
:name "downstair" :char #\>)
*objects*)
(push (make-instance 'obj :x (- *screen-width* 2) :y y2
:name "downstair" :char #\>)
*objects*))))
(defun one-cavern-level ()
"Generate a level with one big cavern and no downstairs"
(dostep (x 1 (- *screen-width* 2))
(dostep (y 1 (- *screen-height* 2))
(let ((type (random-probability 35)))
(setf (aref *map* x y) (make-instance 'tile :blocks type :blocks-sight type)))))
(smooth-dungeon 7)
(gen-pillars)
(let ((x (random (- *screen-width* 2)))
(y (random *screen-height*)))
(while (or (blocked-p x y)
(blocked-p (+ x 2) y))