fogus (owner)

Revisions

gist: 42089 Download_button fork
public
Description:
zombies cl
Public Clone URL: git://gist.github.com/42089.git
Embed All Files: show embed
zombies.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
(comment
Features:
- One monster type: Zombies!
- Teleporters
- Artificial Unintelligence technology for realistic zombie horde
behavior
- Full in-game documentation
- Trees
- Gold (because really, what's a roguelike without gold?)
- Each game is guaranteed to end in death by zombie
 
Release Notes:
- Designed for use with CLISP. Any other Lisp is likely not to work
without modification, due to implementation-specific single-character
input code. If you're trying to alter this program for your Lisp, the
function "get-input-char" is the only thing you need to touch.
- To run the program:
     - install CLISP
     - save the code as "zombies.lisp"
     - type "clisp zombies.lisp" and start running for your life!
- The program is designed to run in an 80x24 terminal. Bigger will
work OK, smaller definitely won't.
 
License:
- This software is released under the Zombie Public License (ZPL).
This means you can use the code for anything you like, but if you
complain about any aspect of it, zombies will eat your BRRRAAAAINS!!!
 
Partial screenshot:
Level: 2 HP: 17 Gold: 10
.......#....#..$.......#..Z...
....................$.........
..............#Z....#.....#Z..
........#...>.Z.....ZZZ.......
...#......@Z...#Z.#..Z.ZZ.....
..........$...........#.......
..............................
)
 
(setf *random-state* (make-random-state t))
 
(defparameter *maxx* 79)
(defparameter *maxy* 22)
 
(defstruct player
  x
  y
  (hp 20)
  (gold 0))
 
(defstruct zombie
  x
  y)
 
(defvar *screen* (make-array (list *maxx* *maxy*)))
(defvar *messages* ())
(defvar *level* 0)
(defvar *player* (make-player))
(defvar *zombies* ())
 
(defun get-input-char ()
  (system::input-character-char
   (EXT:with-keyboard (read-char EXT:*keyboard-input*))))
 
(defun play-game()
  (dotimes (i (1+ *maxy*))
    (format t "~%"))
  (new-level)
  (setf *messages* (cons "Zombies! Press ? for help." nil))
  (print-screen)
  (loop
   (setf *messages* nil)
   (when (process-player)
     (recover-hp)
     (process-zombies)
     (if (< (player-hp *player*) 1)
         (die "You've been killed by zombies."))
     (print-screen))))
 
(defun new-level ()
  (dotimes (i (* 2 *maxy*))
    (format t "~%"))
  (incf *level*)
  (dotimes (i *maxy*)
    (dotimes (j *maxx*)
      (setf (aref *screen* j i) (list #\.))))
  (dotimes (i (random 100))
    (push #\$ (aref *screen* (random *maxx*) (random *maxy*))))
  (dotimes (i (random 10))
    (push #\^ (aref *screen* (random *maxx*) (random *maxy*))))
  (dotimes (i (random 80))
    (push #\# (aref *screen* (random *maxx*) (random *maxy*))))
  (setf *zombies* nil)
  (dotimes (i (+ 8 (* 2 *level*)))
    (let ((x (random *maxx*))
          (y (random *maxy*)))
      (push (make-zombie :x x :y y) *zombies*)
      (push #\Z (aref *screen* x y))))
  (push #\> (aref *screen* (random *maxx*) (random *maxy*)))
  (let ((x (random *maxx*))
        (y (random *maxy*)))
    (setf (player-x *player*) x)
    (setf (player-y *player*) y)
    (push #\@ (aref *screen* x y)))
  (push (format nil "Welcome to Level ~A." *level*) *messages*))
 
(defun print-screen ()
  (format t "~A" #\Return)
  (dotimes (i (1+ *maxx*))
    (format t " "))
  (format t "~A[23A" (code-char 27))
  (format t "~A" #\Return)
  (dotimes (i (1+ *maxx*))
    (format t " "))
  (format t "~A" #\Return)
  (format t "Level: ~A HP: ~A Gold: ~A"
          *level* (player-hp *player*) (player-gold *player*))
  (dotimes (i *maxy*)
    (format t "~%")
    (dotimes (j *maxx*)
      (format t "~A" (car (aref *screen* j i)))))
  (format t "~%")
  (dolist (i (reverse *messages*))
    (format t "~A " i)))
 
(defun process-player ()
  (let ((input (get-input-char))
        (new-x nil)
        (new-y nil))
    (cond ((equal input #\q) (die "Goodbye."))
          ((equal input #\h)
           (setf new-x (- (player-x *player*) 1))
           (setf new-y (player-y *player*)))
          ((equal input #\j)
           (setf new-y (+ (player-y *player*) 1))
           (setf new-x (player-x *player*)))
          ((equal input #\k)
           (setf new-y (- (player-y *player*) 1))
           (setf new-x (player-x *player*)))
          ((equal input #\l)
           (setf new-x (+ (player-x *player*) 1))
           (setf new-y (player-y *player*)))
          ((equal input #\y)
           (setf new-x (- (player-x *player*) 1))
           (setf new-y (- (player-y *player*) 1)))
          ((equal input #\u)
           (setf new-x (+ (player-x *player*) 1))
           (setf new-y (- (player-y *player*) 1)))
          ((equal input #\b)
           (setf new-x (- (player-x *player*) 1))
           (setf new-y (+ (player-y *player*) 1)))
          ((equal input #\n)
           (setf new-x (+ (player-x *player*) 1))
           (setf new-y (+ (player-y *player*) 1)))
          ((equal input #\.)
           (setf new-x (player-x *player*))
           (setf new-y (player-y *player*)))
          ((equal input #\?) (print-help) (return-from process-player nil))
          (t (return-from process-player nil)))
    (when (or (< new-x 0) (>= new-x *maxx*) (< new-y 0) (>= new-y
*maxy*))
      (return-from process-player nil))
    (when (equal (car (aref *screen* new-x new-y)) #\>)
      (new-level)
      (return-from process-player t))
    (let ((gold 0))
      (loop
       (if (not (equal (car (aref *screen* new-x new-y)) #\$))
           (return))
       (incf (player-gold *player*))
       (pop (aref *screen* new-x new-y))
       (incf gold))
      (if (> gold 0)
          (push (format nil "Found ~A gold." gold) *messages*)))
    (when (equal (car (aref *screen* new-x new-y)) #\Z)
      (return-from process-player nil))
    (when (equal (car (aref *screen* new-x new-y)) #\#)
      (return-from process-player nil))
    (when (equal (car (aref *screen* new-x new-y)) #\^)
      (pop (aref *screen* new-x new-y))
      (loop
       (setf new-x (random *maxx*))
       (setf new-y (random *maxy*))
       (if (equal (car (aref *screen* new-x new-y)) #\.)
           (return))))
    (move-player new-x new-y)))
 
(defun move-player (x y)
  (pop (aref *screen* (player-x *player*) (player-y *player*)))
  (setf (player-x *player*) x)
  (setf (player-y *player*) y)
  (push #\@ (aref *screen* x y)))
 
(defun recover-hp ()
  (if (and (= 0 (random 10)) (< (player-hp *player*) 20))
      (incf (player-hp *player*))))
 
(defun process-zombies ()
  (dotimes (z (length *zombies*))
    (if (or (= 0 (random 7)) (not (process-zombie-smart z)))
        (process-zombie-random z))))
 
(defun process-zombie-smart (z)
  (let* ((z-x (zombie-x (nth z *zombies*)))
        (z-y (zombie-y (nth z *zombies*)))
        (p-x (player-x *player*))
        (p-y (player-y *player*))
        (new-x z-x)
        (new-y z-y))
    (cond ((> z-x p-x) (decf new-x))
          ((< z-x p-x) (incf new-x)))
    (cond ((> z-y p-y) (decf new-y))
          ((< z-y p-y) (incf new-y)))
    (when (equal (car (aref *screen* new-x new-y)) #\@)
      (decf (player-hp *player*))
      (push "Ouch!" *messages*)
      (return-from process-zombie-smart t))
    (when (equal (car (aref *screen* new-x new-y)) #\Z)
      (return-from process-zombie-smart nil))
    (when (equal (car (aref *screen* new-x new-y)) #\#)
      (return-from process-zombie-smart nil))
    (when (equal (car (aref *screen* new-x new-y)) #\^)
      (loop
       (setf new-x (random *maxx*))
       (setf new-y (random *maxy*))
       (if (equal (car (aref *screen* new-x new-y)) #\.)
           (return))))
    (move-zombie z new-x new-y)
    t))
 
(defun process-zombie-random (z)
  (let* ((z-x (zombie-x (nth z *zombies*)))
         (z-y (zombie-y (nth z *zombies*)))
         (new-x z-x)
         (new-y z-y)
         (rand-x (random 3))
         (rand-y (random 3)))
    (if (= rand-x 0)
        (incf new-x)
      (if (= rand-x 1)
          (decf new-x)))
    (if (= rand-y 0)
        (incf new-y)
      (if (= rand-y 1)
          (decf new-y)))
    (when (or (< new-x 0) (>= new-x *maxx*) (< new-y 0) (>= new-y
*maxy*))
      (return-from process-zombie-random nil))
    (when (equal (car (aref *screen* new-x new-y)) #\Z)
      (return-from process-zombie-random nil))
    (when (equal (car (aref *screen* new-x new-y)) #\#)
      (return-from process-zombie-random nil))
    (when (equal (car (aref *screen* new-x new-y)) #\^)
      (loop
       (setf new-x (random *maxx*))
       (setf new-y (random *maxy*))
       (if (equal (car (aref *screen* new-x new-y)) #\.)
           (return))))
    (move-zombie z new-x new-y)))
 
(defun move-zombie (z x y)
  (pop (aref *screen*
             (zombie-x (nth z *zombies*))
             (zombie-y (nth z *zombies*))))
  (setf (zombie-x (nth z *zombies*)) x)
  (setf (zombie-y (nth z *zombies*)) y)
  (push #\Z (aref *screen* x y)))
 
(defun die (message)
  (format t "~%~%~A Score: ~A~%~%" message
          (+ (* (player-gold *player*) *level*) (* 10 (- *level* 1))))
  (quit))
 
(defun print-help ()
  (dotimes (i (+ 3 *maxx*))
    (format t "~%"))
  (format t "Welcome to Zombies!~%~%")
  (format t "The world has been overrun by zombies. ")
  (format t "You have no weapons or other means of~%")
  (format t "self-defense. All you can do is run for ")
  (format t "the exit! That, and collect gold.~%~%")
  (format t "Objects:~%@ - You~%Z - Zombies!~%$ - Gold")
  (format t "~%# - Trees~%^ - Teleporters~%")
  (format t "> - Level Exit~%~%Controls:~%y k u~%h @ l")
  (format t "~%b j n~%~%. - Wait~%q - Quit~%~%")
  (format t "Press any key to continue.~%")
  (get-input-char)
  (print-screen))
 
(play-game)