-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasteroidsge.lisp
423 lines (342 loc) · 19.6 KB
/
asteroidsge.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
(ql:quickload "lispbuilder-sdl")
(ql:quickload "lispbuilder-sdl-gfx")
(load "/home/vmanam/Desktop/Repo/Asteroids/neuralnet.lisp")
(defpackage :asteroids
(:use :cl :sdl)
(:export main))
(defparameter *screen-width* 640)
(defparameter *screen-height* 480)
(defparameter *ticks* 0)
(defparameter *ship-wing-angle* 15)
(defparameter *ship-nose-len* -15)
(defparameter *ship-wing-len* 25)
(defparameter *ship-radius* 10)
(defparameter *ship-max-velocity* 500.0) ; weird values because don't know how to properly setup delta-time
(defparameter *ship-acceleration* 100)
(defparameter *ship-acceleration-scaling* .90)
(defparameter *asteroid-max-states* 3)
(defparameter *max-asteroids-spawn* 3)
(defparameter *asteroid-init-speed* 70)
(defparameter *asteroid-init-radius* 30)
(defparameter *bullet-radius* 3)
(defparameter *bullet-velocity* 300)
(defparameter *shoot-delay* 15)
(defparameter *survival-time* 1000)
(defparameter *population* 10)
(defparameter *generations* 10)
(defparameter *mutatation-rate* .2)
(defun get-ticks ()
(let* ((current (sdl:sdl-get-ticks))
(delta (- current *ticks*)))
(setf *ticks* current)
(/ delta 1000.0)))
(defclass ship ()
((angle :accessor angle :initform 90.0 :initarg :angle)
(velocity-angle :accessor velocity-angle :initform 0.0 :initarg ::velocity-angle)
(pos :accessor pos :initform (sdl:point :x 320 :y 240) :initarg :pos)
(acceleration :accessor acceleration :initform 0.0 :initarg :acceleration)
(velocity :accessor velocity :initform 0.0 :initarg :velocity)
(rot-speed :accessor rot-speed :initform 300 :initarg :rot-speed)
(turning :accessor turning :initform nil :initarg :turning)
(thrusting :accessor thrusting :initform nil :initarg :thrusting)
(shooting :accessor shooting :initform nil :initarg :shooting)))
(defclass asteroid ()
((velocity-angle :accessor velocity-angle :initform (+ 0 (random 360)) :initarg :velocity-angle)
(velocity :accessor velocity :initarg :velocity)
(pos :accessor pos :initarg :pos)
(state :accessor state :initarg :state)
(radius :accessor radius :initarg :radius)))
(defclass bullet ()
((velocity-angle :accessor velocity-angle :initarg :velocity-angle)
(velocity :accessor velocity :initform *bullet-velocity* :initarg :velocity)
(pos :accessor pos :initarg :pos)
(radius :accessor radius :initform *bullet-radius* :initarg :radius)))
(defclass world ()
((ship :accessor ship :initform (make-instance 'ship) :initarg :ship)
(asteroids :accessor asteroids :initform nil :initarg :asteroids)
(bullets :accessor bullets :initform nil :initarg :bullets)
(game-over :accessor game-over :initform nil :initarg :game-over)
(neural-net :accessor neural-net :initform (make-instance 'asteroids-neural-net:neural-net :inNodes 3 :hiddenNodes 20 :outNodes 4) :initarg :neural-net)
(delay :accessor delay :initform 0)
(fitness :accessor fitness :initform 0)
(lifetime :accessor lifetime :initform 0)
(shots-fired :accessor shots-fired :initform 1)
(shots-hit :accessor shots-hit :initform 0)
(curr-time :accessor curr-time :initform 0)
(paused :accessor paused :initform nil)))
(defun point-from-angle-length (coord angle len)
(sdl:point :x (+ (sdl:x coord) (* (* len (cos (* angle (/ pi 180)))) -1))
:y (+ (sdl:y coord) (* len (sin (* angle (/ pi 180)))))))
(defun get-angle-in-range (angle)
(cond ((> angle 360) (get-angle-in-range (- angle 360)))
((< angle 0) (get-angle-in-range (+ angle 360)))
(t angle)))
(defmethod render ((ship ship))
(let* ((nose (point-from-angle-length (pos ship) (angle ship) *ship-nose-len*))
(left (point-from-angle-length nose (+ (angle ship) *ship-wing-angle*) *ship-wing-len*))
(right (point-from-angle-length nose (- (angle ship) *ship-wing-angle*) *ship-wing-len*)))
(sdl:draw-line (pos ship) nose :color sdl:*green*)
(sdl:draw-line nose left :color sdl:*white*)
(sdl:draw-line nose right :color sdl:*white*)
(sdl:draw-line left right :color sdl:*white*)))
(defmethod render ((asteroid asteroid))
(sdl:draw-circle (pos asteroid) (radius asteroid) :color sdl:*white*))
(defmethod render ((bullet bullet))
(sdl:draw-circle (pos bullet) (radius bullet) :color sdl:*white*))
(defmethod thrust ((ship ship))
(setf (acceleration ship) (+ (* *ship-acceleration-scaling* (acceleration ship)) 1))
(cond ((> (acceleration ship) *ship-acceleration*) (setf (acceleration ship) *ship-acceleration*)))
(let* ((v2x (+ (* (velocity ship) (cos (* (velocity-angle ship) (/ pi 180)))) (* (acceleration ship) (cos (* (angle ship) (/ pi 180))))))
(v2y (+ (* (velocity ship) (sin (* (velocity-angle ship) (/ pi 180)))) (* (acceleration ship) (sin (* (angle ship) (/ pi 180))))))
(velocity (sqrt (+ (* v2x v2x) (* v2y v2y))))
(velocity-angle (cond ((not (= v2x 0)) (* (atan (/ v2y v2x)) (/ 180 pi))))))
(cond ((> velocity *ship-max-velocity*) (setf (velocity ship) *ship-max-velocity*))
(t (setf (velocity ship) velocity)))
(cond ((= v2x 0) (<= v2y 0) (setf velocity-angle 270))
((= v2x 0) (>= v2y 0) (setf velocity-angle 90))
((and (> v2x 0) (< v2y 0)) (setf velocity-angle (+ velocity-angle 360)))
((and (< v2x 0) (>= v2y 0)) (setf velocity-angle (+ velocity-angle 180)))
((and (< v2x 0) (< v2y 0)) (setf velocity-angle (+ velocity-angle 180))))
(setf (velocity-angle ship) velocity-angle)))
(defmethod stop-thrust ((ship ship))
(setf (acceleration ship) 0.0))
(defmethod shoot ((world world))
(cond ((shooting (ship world))
(cond ((= (mod (delay world) *shoot-delay*) 0)
(setf (bullets world)
(cons
(make-instance 'bullet :velocity-angle (angle (ship world)) :pos (pos (ship world)))
(bullets world)))))
(incf (delay world) 1)
(incf (shots-fired world) 1)
(cond ((> (delay world) *shoot-delay*) (setf (delay world) 0))))))
(defmethod rotate-left ((ship ship))
(setf (rot-speed ship) ((lambda (x)
(cond ((<= x 0) x)
((> x 0) (- 0 x)))) (rot-speed ship))))
(defmethod rotate-right ((ship ship))
(setf (rot-speed ship) (abs (rot-speed ship))))
(defmethod update ((ship ship) delta-time)
(cond ((turning ship) (setf (angle ship) (get-angle-in-range (+ (angle ship) (* (rot-speed ship) delta-time))))))
(cond ((thrusting ship) (thrust ship))
(t (stop-thrust ship)))
(let* ((vx (* (velocity ship) (cos (* (velocity-angle ship) (/ pi 180)))))
(vy (* (velocity ship) (sin (* (velocity-angle ship) (/ pi 180)))))
(dx (* vx delta-time))
(dy (* vy delta-time))
(x (+ (sdl:x (pos ship)) dx))
(y (- (sdl:y (pos ship)) dy)))
(cond ((< x 0) (setf x 640))
((> x 640) (setf x 0)))
(cond ((< y 0) (setf y 480))
((> y 480) (setf y 0)))
(setf (pos ship) (sdl:point :x x :y y)))
(render ship))
(defmethod update ((asteroid asteroid) delta-time)
(let* ((vx (* (velocity asteroid) (cos (* (velocity-angle asteroid) (/ pi 180)))))
(vy (* (velocity asteroid) (sin (* (velocity-angle asteroid) (/ pi 180)))))
(dx (* vx delta-time))
(dy (* vy delta-time))
(x (+ (sdl:x (pos asteroid)) dx))
(y (- (sdl:y (pos asteroid)) dy)))
(cond ((< x 0) (setf x 640))
((> x 640) (setf x 0)))
(cond ((< y 0) (setf y 480))
((> y 480) (setf y 0)))
(setf (pos asteroid) (sdl:point :x x :y y)))
(render asteroid))
(defmethod update ((bullet bullet) delta-time)
(let* ((vx (* (velocity bullet) (cos (* (velocity-angle bullet) (/ pi 180)))))
(vy (* (velocity bullet) (sin (* (velocity-angle bullet) (/ pi 180)))))
(dx (* vx delta-time))
(dy (* vy delta-time))
(x (+ (sdl:x (pos bullet)) dx))
(y (- (sdl:y (pos bullet)) dy)))
(setf (pos bullet) (sdl:point :x x :y y)))
(render bullet))
(defmethod update ((world world) delta-time)
(incf (lifetime world) 1)
(update (ship world) delta-time)
(shoot world)
(dolist (bullet (bullets world))
(update bullet delta-time)
(cond ((< (sdl:x (pos bullet)) 0) (setf (bullets world) (remove bullet (bullets world))))
((> (sdl:x (pos bullet)) 640) (setf (bullets world) (remove bullet (bullets world)))))
(cond ((< (sdl:y (pos bullet)) 0) (setf (bullets world) (remove bullet (bullets world))))
((> (sdl:y (pos bullet)) 480) (setf (bullets world) (remove bullet (bullets world))))))
(dolist (asteroid (asteroids world))
(update asteroid delta-time)
(cond ((> (expt (+ *ship-radius* (radius asteroid)) 2) (+ (expt (- (sdl:x (pos (ship world))) (sdl:x (pos asteroid))) 2)
(expt (- (sdl:y (pos (ship world))) (sdl:y (pos asteroid))) 2)))
(setf (game-over world) t)))
(dolist (bullet (bullets world))
(cond ((> (expt (+ (radius bullet) (radius asteroid)) 2) (+ (expt (- (sdl:x (pos bullet)) (sdl:x (pos asteroid))) 2)
(expt (- (sdl:y (pos bullet)) (sdl:y (pos asteroid))) 2)))
(incf (shots-hit world) 1)
(setf (asteroids world) (remove asteroid (asteroids world)))
(setf (bullets world) (remove bullet (bullets world)))
(cond ((< (state asteroid) 3)
(setf (asteroids world) (append (asteroids world) (create-asteroids (mod (+ (state asteroid) 1) 3)
(+ (state asteroid) 1) (pos asteroid) nil world)))))))))
(let ((num-asteroids 0))
(dolist (asteroid (asteroids world))
(cond ((= (state asteroid) 1) (incf num-asteroids))))
(setf (asteroids world) (append (asteroids world) (create-asteroids (- *max-asteroids-spawn* num-asteroids) 1 nil t world)))))
(defmethod see ((world world))
(let ((direction nil)
(minDis 801))
(dolist (asteroid (asteroids world))
(let* ((xDiff (- (sdl:x (pos asteroid)) (sdl:x (pos (ship world)))))
(yDiff (- (sdl:y (pos asteroid)) (sdl:y (pos (ship world)))))
(distance (sqrt (+ (expt xDiff 2)
(expt yDiff 2))))
(angle (cond ((not (= xDiff 0)) (* (atan (/ yDiff xDiff)) (/ 180 pi))))))
(cond ((= xDiff 0) (<= yDiff 0) (setf angle 270))
((= xDiff 0) (>= yDiff 0) (setf angle 90))
((and (> xDiff 0) (< yDiff 0)) (setf angle (+ angle 360)))
((and (< xDiff 0) (> yDiff 0)) (setf angle (+ angle 180)))
((and (< xDiff 0) (< yDiff 0)) (setf angle (+ angle 180))))
(cond ((< distance minDis)
(setf direction (list (/ distance 1d0) (/ angle 1d0)))))))
(cons (/ (acceleration (ship world)) 1d0) direction)))
(defmethod calculate-fitness ((world world))
(setf (fitness world) (+ (* (shots-hit world) 1000.0) (/ (lifetime world) 10) (expt (/ (shots-hit world) (shots-fired world)) 5.0))))
(defun create-asteroids (num state pos randomp world)
(cond (randomp
(setf pos
(let ((x 0)
(y 0))
(do ((i 0))
((not (= i 0)))
(setf x (random 640))
(setf y (random 480))
(cond ((and (not (= x (sdl:x (pos (ship world))))) (not (= y (sdl:y (pos (ship world)))))) (setf i 1))))
(sdl:point :x x :y y)))))
(let ((asteroids nil))
(dotimes (number num)
(setf asteroids (cons (make-instance 'asteroid :pos pos :state state :velocity (* *asteroid-init-speed* (/ state 1.3)) :radius (/ *asteroid-init-radius* state))
asteroids)))
asteroids))
(defmethod init-asteroids ((world world))
(setf (asteroids world) (create-asteroids *max-asteroids-spawn* 1 nil t world)))
(defmethod play-game ((world world))
(cond ((not (paused world))
(sdl:clear-display sdl:*black*)
(let ((out (asteroids-neural-net:feed (neural-net world) (see world) 3)))
(cond ((> (first out) .8d0) (setf (thrusting (ship world)) t))
(t (setf (thrusting (ship world)) nil)))
(cond ((> (second out) .8d0) (setf (shooting (ship world)) t))
(t (setf (shooting (ship world)) nil)))
(cond ((> (third out) .8d0)
(setf (turning (ship world)) t)
(rotate-left (ship world)))
((> (fourth out) .8d0)
(setf (turning (ship world)) t)
(rotate-right (ship world)))
(t (setf (turning (ship world)) nil))))
(incf (curr-time world))
(update world (get-ticks))
)))
(defmethod alive ((world world))
(and (not (game-over world)) (< (curr-time world) *survival-time*)))
(defun calculate-total-fit (population)
(let ((total-fit 0.0))
(dolist (p population)
(setf total-fit (+ total-fit (fitness p))))
total-fit))
(defun select-random-player (population total-fit)
(let ((rand (random total-fit)))
(dolist (p population)
(cond ((< rand (fitness p)) (return-from select-random-player p))
(t (setf rand (- rand (fitness p))))))))
(defun main ()
(sdl:with-init ()
(sdl:window *screen-width* *screen-height* :title-caption "Asteroids")
(setf (sdl:frame-rate) 60)
(let* ((world (make-instance 'world))
(init t)
(curr-pop 0)
(curr-gen 0)
(population (list world))
(best-fitness -1)
(best-player nil))
(init-asteroids world)
(asteroids-neural-net:init (neural-net world))
(sdl:with-events ()
(:quit-event () t)
(:key-down-event (:key key)
(case key
(:sdl-key-a
(setf (turning (ship world)) t)
(rotate-right (ship world)))
(:sdl-key-d
(setf (turning (ship world)) t)
(rotate-left (ship world)))
(:sdl-key-space
(setf (thrusting (ship world)) t))
(:sdl-key-p
(setf (paused world) (not (paused world))))
(:sdl-key-f
(setf (shooting (ship world)) t))
(:sdl-key-l
(let ((net (asteroids-neural-net:read-net "/home/vmanam/Desktop/Repo/Asteroids/out.txt"))
(new-world (make-instance 'world)))
(setf (neural-net new-world) net)
(setf world new-world)
(init-asteroids world)))))
(:key-up-event (:key key)
(case key
(:sdl-key-a
(setf (turning (ship world)) nil))
(:sdl-key-d
(setf (turning (ship world)) nil))
(:sdl-key-space
(setf (thrusting (ship world)) nil))
(:sdl-key-f
(setf (shooting (ship world)) nil))))
(:idle ()
(cond ((alive world) (play-game world))
((and (not (alive world)) (< curr-pop *population*))
(cond ((> (calculate-fitness world) best-fitness)
(asteroids-neural-net:save (neural-net world) "/home/vmanam/Desktop/Repo/Asteroids/out.txt")
(setf best-fitness (calculate-fitness world))
(setf best-player (neural-net world))))
(sdl:clear-display sdl:*black*)
(incf curr-pop)
(cond ((and init (< curr-pop *population*))
(setf world (make-instance 'world))
(setf population (append population (list world)))
(init-asteroids world)
(asteroids-neural-net:init (neural-net world)))
((<= curr-pop *population*) (setf world (nth (- curr-pop 1) population)) (init-asteroids world))))
((and (not (alive world)) (>= curr-pop *population*) (< curr-gen *generations*))
(sdl:clear-display sdl:*black*)
(let ((best-world (make-instance 'world))
(new-pop nil))
(setf (neural-net best-world) best-player)
(setf new-pop (list best-world))
(do ((i 1 (+ i 1)))
((>= i *population*))
(cond ((< i (/ *population* 2))
(let* ((p (select-random-player population (calculate-total-fit population)))
(net (asteroids-neural-net:mutate (neural-net p) *mutatation-rate*))
(new-p (make-instance 'world)))
(setf (neural-net new-p) net)
(setf new-pop (append new-pop (list new-p)))))
(t
(let* ((p1 (select-random-player population (calculate-total-fit population)))
(p2 (select-random-player population (calculate-total-fit population)))
(child (asteroids-neural-net:crossover (neural-net p1) (neural-net p2)))
(child-mutated (asteroids-neural-net:mutate child *mutatation-rate*))
(p (make-instance 'world)))
(setf (neural-net p) child-mutated)
(setf new-pop (append new-pop (list p)))))))
(setf population new-pop)
(setf init nil)
(incf curr-gen)
(setf curr-pop 1)
(setf world (first population))
(init-asteroids world)))
((and (not (alive world)) (>= curr-pop *population*) (>= curr-gen *generations*)) (sdl:clear-display sdl:*black*)))
(sdl:update-display))))))
(main)