-
Notifications
You must be signed in to change notification settings - Fork 4
/
player.lisp
49 lines (44 loc) · 1.58 KB
/
player.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
;; for player
(in-package :org.xzpeter.game.starwar)
(defmethod player-planets ((player player))
"find all the planets that belongs to player"
(let ((res nil))
(dolist (planet *planet-list*)
(when (eq (player planet) player)
(setq res (cons planet res))))
res))
(defmethod power ((player player))
"get the power of the player"
(loop for planet in (player-planets player) sum (power planet)))
(defmethod nearest-planets ((player player) n)
"get the nearest N planets that is near the player's planet. "
(let ((my-planets (player-planets player))
(res nil))
(dolist (planet my-planets)
(let ((enemy-planets-sorted
(nreverse (delete-if #'(lambda (p) (eq (player p) player))
(planet-sort-by-distance planet)))))
(setq res
(nconc res
(nreverse (nthcdr
(let ((v (- (length enemy-planets-sorted)
n)))
(if (< v 0) 0 v))
enemy-planets-sorted))))))
(setq res (delete-duplicates res))
(planet-sort-by-player res)))
(defmethod update-player-ai-lv1 ((player player))
"first attempt of AI: find nearest planet which is smaller, and conqure
it"
(let* ((own-power (power player))
(nearest-planets (nearest-planets player 3))
(target (dolist (planet nearest-planets)
(when (> own-power (+ 30 (life planet) (power planet)))
;; if we use full power, we mostly can conqure the planet.
(return planet)))))
(when target
(dolist (planet (player-planets player))
(transport-stars planet target)))))
(defmethod update-player-ai ((player player))
"update the AI logic"
(update-player-ai-lv1 player))