-
Notifications
You must be signed in to change notification settings - Fork 0
/
orc-battle.el
363 lines (288 loc) · 10.5 KB
/
orc-battle.el
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
;; (defclass record () ; No superclasses
;; ((name :initarg :name
;; :initform ""
;; :type string
;; :custom string
;; :documentation "The name of a person.")
;; (birthday :initarg :birthday
;; :initform "Jan 1, 1970"
;; :custom string
;; :type string
;; :documentation "The person's birthday.")
;; (phone :initarg :phone
;; :initform ""
;; :documentation "Phone number."))
;; "A single record for tracking people I know.")
;; (setq rec (record "rand" :name "Random Sample" :birthday "01/01/2000" :phone "555-5555"))
;; (type-of rec)
;; (record-p rec)
;; (oref rec :birthday)
;; (oset rec :phone "555-5566")
;; (oref rec :phone)
;; (defmethod call-record ((rec record) &optional scriptname)
;; "Dial the phone for the record REC.
;; Execute the program SCRIPTNAME as to dial the phone."
;; (message "Dialing the phone for %s" (oref rec name))
;; ;; to be implemented...
;; )
;; (call-record rec)
;; (defclass abroad-record (record)
;; ((country :initarg :country
;; :initform "DE"
;; :documentation "Country this person is living in."))
;; "Special class for people living abroad.")
;; (defmethod call-record :before ((rec abroad-record) &optional scriptname)
;; "Prepend country code to phone number, then dial the phone for REC.
;; Execute the program SCRIPTNAME as to dial the phone"
;; (message "Prepending country code to phone number.")
;; (unless (string-match "^00" (oref rec :phone))
;; (let ((country (oref rec :country)))
;; (cond
;; ;; just an example...
;; ((string= country "IT")
;; (oset rec :phone (concat "0043" (oref rec :phone)))))))
;; )
;; (setq abroadrec (abroad-record "friend" :name "Good Friend" :birthday "01/01/2000" :phone "555-5555" :country "IT"))
;; (call-record abroadrec)
;; (call-next-method)
;; (eieio-customize-object rec)
;; Change defstruct to defclass
(defvar *player-health* nil)
(defvar *player-agility* nil)
(defvar *player-strength* nil)
(defvar *monsters* nil)
(defvar *monster-builders* nil)
(defvar *monster-num* 12)
(defun orc-battle ()
(init-monsters)
(init-player)
(game-loop)
(when (player-dead)
(princ "You have been killed. Game Over."))
(when (monsters-dead)
(princ "Congratulations! You have vanquished all of your foes.")))
(defun game-loop ()
(unless (or (player-dead) (monsters-dead))
(show-player)
(dotimes (k (1+ (truncate (/ (max 0 *player-agility*) 15))))
(unless (monsters-dead)
(show-monsters)
(player-attack)))
;; (fresh-line)
(map 'list
(lambda(m)
(or (monster-dead m) (monster-attack m)))
*monsters*)
(game-loop)))
(defun init-player ()
(setf *player-health* 30)
(setf *player-agility* 30)
(setf *player-strength* 30))
;; (init-player)
(defun player-dead ()
(<= *player-health* 0))
;; (player-dead)
(defun show-player ()
(princ "\nYou are a valiant knight with a health of ")
(princ *player-health*)
(princ ", an agility of ")
(princ *player-agility*)
(princ ", and a strength of ")
(princ *player-strength*))
;; (show-player)
(defun player-attack ()
;; (fresh-line)
;; (princ "Attack style: [s]tab [d]ouble swing [r]oundhouse:")
(let ((command (read-from-minibuffer "Attack style: [s]tab [d]ouble swing [r]oundhouse:")))
(cond
((string-equal "s" command) (monster-hit (pick-monster)
(+ 2 (randval (ash *player-strength* -1)))))
((string-equal "d" command) (let ((x (randval (truncate (/ *player-strength* 6)))))
(princ "Your double swing has a strength of ")
(princ x)
(monster-hit (pick-monster) x)
(unless (monsters-dead)
(monster-hit (pick-monster) x))))
((string-equal "r" command) (dotimes (x (1+ (randval (truncate (/ *player-strength* 3)))))
(unless (monsters-dead)
(monster-hit (random-monster) 1)))))))
;; (setq temp (read-from-minibuffer "Attack style: [s]tab [d]ouble swing [r]oundhouse:") )
;; (cond
;; ((string-equal "s" temp) (message "a")))
;; (eql (read-from-minibuffer "Attack style: [s]tab [d]ouble swing [r]oundhouse:") "s")
(orc-battle)
;; (player-attack)
(defun randval (n)
(1+ (random (max 1 n))))
(defun random-monster ()
(let ((m (aref *monsters* (random (length *monsters*)))))
(if (monster-dead m)
(random-monster)
m)))
(defun pick-monster ()
(let ((x (string-to-number (read-from-minibuffer "Monster #:"))))
(if (not (and (integerp x) (>= x 1) (<= x *monster-num*)))
(progn (princ "That is not a valid monster number.")
(pick-monster))
(let ((m (aref *monsters* (1- x))))
(if (monster-dead m)
(progn (princ "That monster is already dead.")
(pick-monster))
m)))))
(defun init-monsters ()
(setf *monsters*
(map 'vector
(lambda (x)
(funcall (nth (random (length *monster-builders*))
*monster-builders*)))
(make-array *monster-num*))))
;; (init-monsters)
;; (random (length *monster-builders*))
(defun monster-dead (m)
(<= (monster-health m) 0))
(defun monsters-dead ()
(every #'monster-dead *monsters*))
(defun show-monsters ()
(princ "Your foes:")
(let ((x 0))
(map 'list
(lambda (m)
(princ " ")
(princ (incf x))
(princ ". ")
(if (monster-dead m)
(princ "**dead**")
(progn (princ "(Health=")
(princ (monster-health m))
(princ ") ")
(monster-show m))))
*monsters*)))
;; (show-monsters)
(defstruct monster (health (randval 10)))
;; (elt (make-monster) 0)
;; (defstruct test fun)
;; (setq dave (make-test :fun (lambda ()
;; (message "test"))))
;; (funcall (test-fun dave))
;; (message dave-fun)
;; (make-monster)
;; vector 0:name 1:healthy 3:.....
(defun monster-type-of (m)
(if (vectorp m)
(case (elt m 0)
('cl-struct-monster "monster")
('cl-struct-orc "orc")
('cl-struct-hydra "hydra")
('cl-struct-slime-mold "slime-mold")
('cl-struct-brigand "brigand"))
"unknow monster type"))
(defmethod monster-hit-monster (m x)
(decf (monster-health m) x)
(if (monster-dead m)
(progn (princ "You killed the ")
(princ (monster-type-of m))
(princ "! "))
(progn (princ "You hit the ")
(princ (monster-type-of m))
(princ ", knocking off ")
(princ x)
(princ " health points! "))))
(defmethod monster-hit (m x)
(if (vectorp m)
(case (elt m 0)
('cl-struct-monster (monster-hit-monster m x))
('cl-struct-orc (monster-hit-monster m x))
('cl-struct-hydra (monster-hit-hydra m x))
('cl-struct-slime-mold (monster-hit-monster m x))
('cl-struct-brigand (monster-hit-monster m x)))
"unknow monster type"))
(defmethod monster-show-monster (m)
(princ "A fierce ")
(princ (monster-type-of m)))
(defun monster-show (m)
(if (vectorp m)
(case (elt m 0)
('cl-struct-monster (monster-show-monster m))
('cl-struct-orc (monster-show-orc m))
('cl-struct-hydra (monster-show-hydra m))
('cl-struct-slime-mold (monster-show-slime-mold m))
('cl-struct-brigand (monster-show-monster m)))
"unknow monster type")
(defmethod monster-attack-monster (m))
(defmethod monster-attack (m)
(if (vectorp m)
(case (elt m 0)
('cl-struct-monster (monster-attack-monster m))
('cl-struct-orc (monster-attack-orc m))
('cl-struct-hydra (monster-attack-hydra m))
('cl-struct-slime-mold (monster-attack-slime-mold m))
('cl-struct-brigand (monster-attack-brigand m)))
"unknow monster type"))
(defstruct (orc (:include monster)) (club-level (randval 8)))
;; (setq dave (make-orc))
;; (monster-health dave)
;; (type-of (elt dave 0))
;; (monster-type-of dave)
;; (funcall (orc-monster_show dave) dave)
;; (make-orc)
;; (type-of (make-monster))
;; (type-of (make-orc))
(push #'make-orc *monster-builders*)
(defmethod monster-show-orc (m)
(princ "A wicked orc with a level ")
(princ (orc-club-level m))
(princ " club"))
(defmethod monster-attack-orc (m)
(let ((x (randval (orc-club-level m))))
(princ "An orc swings his club at you and knocks off ")
(princ x)
(princ " of your health points. ")
(decf *player-health* x)))
(defstruct (hydra (:include monster)))
(push #'make-hydra *monster-builders*)
(defmethod monster-show-hydra (hydra)
(princ "A malicious hydra with ")
(princ (monster-health m))
(princ " heads."))
(defmethod monster-hit-hydra (m x)
(decf (monster-health m) x)
(if (monster-dead m)
(princ "The corpse of the fully decapitated and decapacitated hydra falls to the floor!")
(progn (princ "You lop off ")
(princ x)
(princ " of the hydra's heads! "))))
(defmethod monster-attack-hydra (m)
(let ((x (randval (ash (monster-health m) -1))))
(princ "A hydra attacks you with ")
(princ x)
(princ " of its heads! It also grows back one more head! ")
(incf (monster-health m))
(decf *player-health* x)))
(defstruct (slime-mold (:include monster)) (sliminess (randval 5)))
(push #'make-slime-mold *monster-builders*)
(defmethod monster-show-slime-mold (m)
(princ "A slime mold with a sliminess of ")
(princ (slime-mold-sliminess m)))
(defmethod monster-attack-slime-mold (m)
(let ((x (randval (slime-mold-sliminess m))))
(princ "A slime mold wraps around your legs and decreases your agility by ")
(princ x)
(princ "! ")
(decf *player-agility* x)
(when (zerop (random 2))
(princ "It also squirts in your face, taking away a health point! ")
(decf *player-health*))))
(defstruct (brigand (:include monster)))
(push #'make-brigand *monster-builders*)
(defmethod monster-attack-brigand (m)
(let ((x (max *player-health* *player-agility* *player-strength*)))
(cond ((= x *player-health*)
(princ "A brigand hits you with his slingshot, taking off 2 health points! ")
(decf *player-health* 2))
((= x *player-agility*)
(princ "A brigand catches your leg with his whip, taking off 2 agility points! ")
(decf *player-agility* 2))
((= x *player-strength*)
(princ "A brigand cuts your arm with his whip, taking off 2 strength points! ")
(decf *player-strength* 2)))))
;; (orc-battle)