Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 220 lines (183 sloc) 6.492 kB
b216a2a orc-battle.lisp
Andrey Paramonov authored
1 (defparameter *player-health* nil)
2 (defparameter *player-agility* nil)
3 (defparameter *player-strength* nil)
4
5 (defparameter *monsters* nil)
6 (defparameter *monster-builders* nil)
7 (defparameter *monster-num* 12)
8
9 (defun orc-battle ()
10 (init-monsters)
11 (init-player)
12 (game-loop)
13 (when (player-dead)
14 (princ "You have been killed. Game Over."))
15 (when (monsters-dead)
16 (princ "Congratulations! You have vanquished all of your foes.")))
17
18 (defun init-player ()
19 (setf *player-health* 30)
20 (setf *player-agility* 30)
21 (setf *player-strength* 30))
22
23 (defun game-loop ()
24 (unless (or (player-dead) (monsters-dead))
25 (show-player)
26 (dotimes (k (1+ (truncate (/ (max 0 *player-agility*) 15))))
27 (unless (monsters-dead)
28 (show-monsters)
29 (player-attack)))
30 (fresh-line)
31 (map 'list
32 (lambda (m) (or (monster-dead m) (monster-attack m)))
33 *monsters*)
34 (game-loop)))
35
36 (defun player-dead ()
37 (<= *player-health* 0))
38
39 (defun show-player ()
40 (fresh-line)
41 (princ "You are a valiant knight with a health of ")
42 (princ *player-health*)
43 (princ ", an agility of ")
44 (princ *player-agility*)
45 (princ ", and a strength of ")
46 (princ *player-strength*))
47
48 (defun player-attack ()
49 (fresh-line)
50 (princ "Attack style: [s]tab [d]ouble swing [r]oundhouse:")
51 (case (read)
52 (s (monster-hit (pick-monster)
53 (+ 2 (randval (ash *player-strength* -1)))))
d1ddf54 Changed according to http://groups.google.com/group/land-of-lisp/brow…
Andrey Paramonov authored
54 (d (let ((x (randval (truncate *player-strength* 6))))
b216a2a orc-battle.lisp
Andrey Paramonov authored
55 (princ "Your double swing has a strength of ")
56 (princ x)
57 (fresh-line)
58 (monster-hit (pick-monster) x)
59 (unless (monsters-dead)
60 (monster-hit (pick-monster) x))))
d1ddf54 Changed according to http://groups.google.com/group/land-of-lisp/brow…
Andrey Paramonov authored
61 (otherwise (dotimes (x (1+ (randval (truncate *player-strength* 3))))
b216a2a orc-battle.lisp
Andrey Paramonov authored
62 (unless (monsters-dead)
63 (monster-hit (random-monster) 1))))))
64
65 (defun pick-monster ()
66 (fresh-line)
67 (princ "Monster #:")
68 (let ((x (read)))
69 (if (not (and (integerp x) (>= x 1) (<= x *monster-num*)))
70 (progn (princ "That is not a valid monster number.")
71 (pick-monster))
72 (let ((m (aref *monsters* (1- x))))
73 (if (monster-dead m)
74 (progn (princ "That monster is alread dead.")
75 (pick-monster))
76 m)))))
77
78 (defun randval (n)
79 (1+ (random (max 1 n))))
80
81 (defun random-monster ()
82 (let ((m (aref *monsters* (random (length *monsters*)))))
83 (if (monster-dead m)
84 (random-monster)
85 m)))
86
87 (defun init-monsters ()
88 (setf *monsters*
89 (map 'vector
90 (lambda (x)
91 (funcall (nth (random (length *monster-builders*))
92 *monster-builders*)))
93 (make-array *monster-num*))))
94
95 (defun monster-dead (m)
96 (<= (monster-health m) 0))
97
98 (defun monsters-dead ()
99 (every #'monster-dead *monsters*))
100
101 (defun show-monsters ()
102 (fresh-line)
103 (princ "Your foes:")
104 (let ((x 0))
105 (map 'list
106 (lambda (m)
107 (fresh-line)
108 (princ " ")
109 (princ (incf x))
110 (princ ". ")
111 (if (monster-dead m)
112 (princ "**dead**")
113 (progn (princ "(Health=")
114 (princ (monster-health m))
115 (princ ") ")
116 (monster-show m))))
117 *monsters*)))
118
119 ; Monsters
120
121 (defstruct monster (health (randval 10)))
122
123 (defmethod monster-hit (m x)
124 (decf (monster-health m) x)
125 (if (monster-dead m)
126 (progn (princ "You killed the ")
127 (princ (type-of m))
128 (princ "! "))
129 (progn (princ "You hit the ")
130 (princ (type-of m))
131 (princ ", knocking off ")
132 (princ x)
133 (princ " health points! "))))
134
135 (defmethod monster-show (m)
136 (princ "A fierce ")
137 (princ (type-of m)))
138
139 (defmethod monster-attack (m))
140
141 ; Orcs
142
143 (defstruct (orc (:include monster)) (club-level (randval 8)))
144 (push #'make-orc *monster-builders*)
145
146 (defmethod monster-show ((m orc))
147 (princ "A wicked orc with a level ")
148 (princ (orc-club-level m))
149 (princ " club"))
150
151 (defmethod monster-attack ((m orc))
152 (let ((x (randval (orc-club-level m))))
153 (princ "An orc swings his club at you and knocks off ")
154 (princ x)
155 (princ " of your health points. ")
156 (decf *player-health* x)))
157
158 ; Hydra
159
160 (defstruct (hydra (:include monster)))
161 (push #'make-hydra *monster-builders*)
162
163 (defmethod monster-show ((m hydra))
164 (princ "A malicious hydra with ")
165 (princ (monster-health m))
166 (princ " heads."))
167
168 (defmethod monster-hit ((m hydra) x)
169 (decf (monster-health m) x)
170 (if (monster-dead m)
171 (princ "The corpse of the fully decapitated and decapacitated hydra falls to the floor!")
172 (progn (princ "You lop off ")
173 (princ x)
174 (princ " of the hydra's heads! "))))
175
176 (defmethod monster-attack ((m hydra))
177 (let ((x (randval (ash (monster-health m) -1))))
178 (princ "A hydra attacks you with ")
179 (princ x)
180 (princ " of its heads! It also grows back one more head! ")
181 (incf (monster-health m))
182 (decf *player-health* x)))
183
184 ; Slime Mold
185
186 (defstruct (slime-mold (:include monster)) (sliminess (randval 5)))
187 (push #'make-slime-mold *monster-builders*)
188
189 (defmethod monster-show ((m slime-mold))
190 (princ "A slime mold with a sliminess of ")
191 (princ (slime-mold-sliminess m)))
192
193 (defmethod monster-attack ((m slime-mold))
194 (let ((x (randval (slime-mold-sliminess m))))
195 (princ "A slime mold wraps around your legs and decreases your agility by ")
196 (princ x)
197 (princ "! ")
198 (decf *player-agility* x)
199 (when (zerop (random 2))
200 (princ "It also squirts in your face, taking away a health point! ")
201 (decf *player-health*))))
202
203 ; Cunning Brigand
204
205 (defstruct (brigand (:include monster)))
206 (push #'make-brigand *monster-builders*)
207
208 (defmethod monster-attack ((m brigand))
209 (let ((x (max *player-health* *player-agility* *player-strength*)))
210 (cond ((= x *player-health*)
211 (princ "A brigand hits you with his slingshot, taking off 2 health points! ")
212 (decf *player-health* 2))
213 ((= x *player-agility*)
214 (princ "A brigand catches your leg with his whip, taking off 2 agility points! ")
215 (decf *player-agility* 2))
216 ((= x *player-strength*)
217 (princ "A brigand cuts your arm with his whip, taking off 2 strength points! ")
218 (decf *player-strength* 2)))))
219
Something went wrong with that request. Please try again.