-
Notifications
You must be signed in to change notification settings - Fork 4
/
globals.lisp
205 lines (182 loc) · 6.48 KB
/
globals.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
;; variables definitions
(in-package :org.xzpeter.game.starwar)
(defparameter *debug-string* "")
(defvar color-grey (sdl:color :r 80 :g 80 :b 80))
(defparameter screen-width 800)
(defparameter screen-height 600)
(defparameter bg-color sdl:*black*)
(defparameter game-frame-color sdl:*white*)
(defparameter fullscreen nil)
(defparameter frame-rate 60)
(defparameter scroll-speed 20)
(defvar *game-over* nil)
(defvar *news* "")
(defvar *player1* nil)
(defvar *player2* nil)
(defvar *players* nil)
(defvar *planet-list* nil)
(defvar *running* nil)
(defvar *paused* nil)
(defvar *show-arrow* nil)
(defvar *selected-planet* nil)
(defvar *star-count* 0
"how many stars in the world")
(defvar *bg-stars* nil
"the stars as background.")
;; in this game, I am preparing to be able to only display a little part of
;; the world map, and user can navigate by putting the mouse cursor to the
;; edge of the screen
;; *screen-pos* points to the lefttop of the current screen
(defvar *screen-pos-x* 0)
(defvar *screen-pos-y* 0)
(defparameter planet-smallest 30)
(defparameter planet-biggest 150)
(defparameter planet-default-acc-max 120)
(defparameter planet-distance-min 20
"the minimun distance between two planet. currently only used when auto
generateing the map. ")
(defparameter planet-total 15
"Total number of planets. Only used when auto generating maps. ")
(defparameter planet-neutral-stars-max 10
"max stars one neutral planet can have")
;; these defines the size of the world
(defparameter world-leftmost 0)
(defparameter world-topmost 0)
(defparameter world-rightmost 1600)
(defparameter world-bottommost 1200)
;; these params defines the biggest places that user can go via moving the
;; mouse
(defparameter screen-leftmost world-leftmost)
(defparameter screen-topmost world-topmost)
(defparameter star-life-max 10)
(defparameter star-life-min 3)
(defparameter star-speed-max 3.0)
(defparameter star-speed 1.5)
(defparameter star-speed-min 0.5)
(defparameter star-tail-len 15)
(defparameter star-dist-max-from-home 20
"how far can a star moves away from its owner")
;; when the mouse moves to the edge of the screen, we should move the screen
;; position on the worldmap.
;; these are the margin place, outside which the moving is activated.
(defparameter margin-left 10)
(defparameter margin-top 10)
;; defines the background
(defparameter bg-star-size-max 2)
(defparameter bg-star-size-min 1)
(defparameter bg-star-n 50)
(defparameter *star-color-selected* sdl:*yellow*)
(defparameter star-safe-distance 40)
(defparameter planet-life-regeneration-speed 0.01)
(defparameter planet-core-radius 3)
(defparameter star-max-amount 1000)
(defparameter screen-rightmost (- world-rightmost screen-width))
(defparameter screen-bottommost (- world-bottommost screen-height))
(defparameter world-width (- world-rightmost world-leftmost))
(defparameter world-height (- world-bottommost world-topmost))
(defparameter margin-bottom (- screen-height 10))
(defparameter margin-right (- screen-width 10))
(defun generate-bg-stars (n)
"generate N bg stars and return list"
(loop repeat n collect
(list (random world-width) (random world-height)
(+ bg-star-size-min (random (- bg-star-size-max
bg-star-size-min -1))))))
(defun load-file-set-parameters (filename)
"read one file and load all the parameters"
;; here, we MUST make sure that we are in the RIGHT package (that is the
;; starwar package. or the parameters will not be set!
(with-open-file (s filename)
(do ((form (read s) (read s nil 'eof)))
((eq form 'eof) nil)
(if (not (= (length form) 2))
(error "configure file format not right!"))
(setq form (cons 'defparameter form))
(format t "eval: ~a~%" form)
(eval form))))
(defun auto-generate-planets (amount)
"auto generate planets and normally set the *planet-list* var.
AMOUNT is how many planet to generate in all,
INIT-AMOUNT is how many planets one player own at the beginning of game"
(let ((width world-width)
(height world-height)
(total 10000)
(count 0)
(vlist nil))
(labels ((x (v) (nth 0 v))
(y (v) (nth 1 v))
(r (v) (nth 2 v))
(outside-p (v)
(or (< (- (x v) (r v)) world-leftmost)
(> (+ (x v) (r v)) world-rightmost)
(< (- (y v) (r v)) world-topmost)
(> (+ (y v) (r v)) world-bottommost)))
(collide-list (v vlist)
(dolist (n vlist)
(when
(distance-less-than-p (x v) (y v)
(x n) (y n)
(+ (r v) (r n)
planet-distance-min))
(return t)))))
(loop do
(progn
(decf total)
;; overflow, no good result
(when (< total 0)
(error "failed auto generate map! please retry!"))
;; prepare for the output of list
(when (>= count amount)
(return (loop for v in vlist collect
(make-instance 'planet
:x (x v)
:y (y v)
:r (r v)
:player nil))))
(let ((v (list (random width)
(random height)
(+ planet-smallest
(random
(- planet-biggest planet-smallest))))))
(when (and (not (collide-list v vlist))
(not (outside-p v)))
(incf count)
(setq vlist (cons v vlist)))))))))
(defun clear-global-vars ()
;; without this, all the random numbers will be the same after saving
;; the lisp core image
(setq *random-state* (make-random-state t))
(setq *planet-list* nil)
(setq *running* nil)
(setq *paused* nil
*game-over* nil)
(setq *show-arrow* nil)
(setq *selected-planet* nil)
(setq *star-count* 0)
(setq *player1* ; the real player
(make-instance 'player
:planet-color sdl:*blue*
:star-color (color-lighten sdl:*blue* 0.6)))
(setq *player2* ; the COM (temporarily)
(make-instance 'player
:planet-color sdl:*red*
:star-color (color-lighten sdl:*red* 0.6)
:use-ai t))
(setq *players* (list *player1* *player2*))
(setq *screen-pos-x* 0
*screen-pos-y* 0
star-speed 1.5)
(setq *news* ""
*bg-stars* (generate-bg-stars bg-star-n)
*planet-list* (planet-sort-by-size
(auto-generate-planets planet-total)))
(setf (player (fifth *planet-list*)) *player1*)
(setf (player (sixth *planet-list*)) *player2*)
;; load all the configure file
(load-file-set-parameters "starwar.conf")
(setq screen-rightmost (- world-rightmost screen-width))
(setq screen-bottommost (- world-bottommost screen-height))
(setq world-width (- world-rightmost world-leftmost))
(setq world-height (- world-bottommost world-topmost))
(setq margin-bottom (- screen-height 10))
(setq margin-right (- screen-width 10)))