File: gblocks.lsp

package info (click to toggle)
xlispstat 3.52.14-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,560 kB
  • ctags: 12,676
  • sloc: ansic: 91,357; lisp: 21,759; sh: 1,525; makefile: 521; csh: 1
file content (386 lines) | stat: -rw-r--r-- 11,414 bytes parent folder | download | duplicates (4)
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
; Blocks World from Winston&Horn
; modified for XLISP and graphics by Tom Almy


#-:classes (load "classes")

;
; Functions for graphic assistance

(defvar *bx* 0)		; text communication region
(defvar *by* 21)
(defvar *gx* 50)	; Graphic region origin
(defvar *gy* 100)
(defvar *ymax* 349)	; height of display
(defvar *char-width* 8)	; width of characters
(defvar *char-height* 14)	; height of characters
(defvar *step-size* 10)	; lcd of block widths 
(defvar *delay-time* 0.3)	; delay time in seconds


; Move the cursor to nearest position to graphic coordiates
#+:math (defun setgpos (x y)
     (goto-xy (round (+ x *gx*) *char-width*)
	      (round (- *ymax* y *gy*) *char-height*)))
#-:math (defun setgpos (x y)
     (goto-xy (truncate (/ (+ x *gx*) *char-width*))
	     (truncate (/ (+ (/ *char-height* 2) (- *ymax* y *gy*)) 
			  *char-height*))))

; Move the cursor to the currently set bottom position and clear the line
;  under it
(defun bottom ()
    (goto-xy *bx* (+ *by* 1))
    (cleol)
    (goto-xy *bx* *by*)
    (cleol)
    (goto-xy *bx* (- *by* 1))
    (cleol)
    (color 15)  ; Force color to white
    nil)

; Clear the screen and go to the bottom
(defun cb ()
    (cls)
    (bottom))


; Go to graphics mode
(defun gmode () 
       (mode 16)
       (setq *by* 21)
       (setq *ymax* 349) ; reset defaults
       (setq *char-height* 14))

(defun gmode480 () ; this is for GENOA SuperEGA HiRes+
       (mode 115 115 640 480)
       (setq *ymax* 480)
       (setq *by* 21)
       (setq *char-height* 8))

(defun gmode600 () ; this is for GENOA SuperEGA HiRes+
       (mode 121 121 800 600)
       (setq *by* 21)
       (setq *ymax* 600)
       (setq *char-height* 8))

(defun gmodev () ; EVEREX 640x480 mode
       (setq *by* 21)
       (mode 112 0 640 480)
       (setq *ymax* 480)
       (setq *char-height* 14)
       (display-blocks))

(defun gmodeVGA800 () ; this is for Video 7 FastWrite/VRAM VGA
       (mode 28421 98 800 600)
       (setq *by* 21)
       (setq *ymax* 600)
       (setq *char-height* 8)
       (display-blocks))

(defun gmodeVGA (&aux dims) ; standard 640x480 VGA
			    ; Modified so it will work in Windows as well
       (setq dims (mode 18))
       (setq *ymax* (1+ (fourth dims)))
       (setq *by* 9)
#+:math  (setq *char-height* (truncate (1+ (fourth dims)) (second dims)))
#+:math  (setq *char-width* (truncate (1+ (third dims)) (first dims)))
#-:math  (setq *char-height* (truncate (/ (1+ (fourth dims)) (second dims))))
#-:math  (setq *char-width* (truncate (/ (1+ (third dims)) (first dims))))
       (setq *gy* (truncate (* 2.5 *char-height*)))
       (display-blocks))

; abstract classes for ball types

; basic blocks support nothing
(defclass basic-block (name color width height position supported-by))

(defmethod basic-block :support-for () nil)

(defmethod basic-block :top-location  () 
	(list (+ (first position) (/ width 2))
	      (+ (second position) height)))

(defmethod basic-block :drawname ()
	(setgpos (+ (first position) 
		    (/ (- width (* *char-width* (flatc name))) 2))
	         (+ (second position) (/ height 2)))
        (color color) ; For Windows, which does color text
	(princ name))

(defmethod basic-block :undrawname ()
	(setgpos (+ (first position) 
		    (/ (- width (* *char-width* (flatc name))) 2)) 
	         (+ (second position) (/ height 2)))
	(dotimes (i (flatc name)) (princ " ")))

(defmethod basic-block :draw ()
	(color (+ color 128))
	(move (+ *gx* (first position)) (+ *gy* (second position)))
	(drawrel (1- width) 0 
		 0 (1- height)
		 (- 1 width) 0 
		 0 (- 1 height)))

; movable-blocks can be moved
(defclass movable-block () () basic-block)

(defmethod movable-block :new-position (newpos)
	(send self :draw)
	(send self :undrawname)
	(setf position newpos)
	(send self :drawname)
	(send self :draw))

; load-bearing blocks can support other blocks, and can be moved
(defclass load-bearing-block (support-for) () movable-block)

; we can't have multiple inheritance, so we need a separate class for table
; table blocks can support other blocks but cannot be moved.

(defclass table-block (support-for) () basic-block)

; Specific classes for table brick wedge and ball

(defclass brick () () load-bearing-block)

(defclass wedge () () movable-block)

(defmethod wedge :draw ()
	(color (+ color 128))
	(move (+ *gx* (first position)) (+ *gy* (second position)))
	(drawrel (1- width) 0 
		 (- 1 (/ width 2)) (1- height )
		 (- (/ width 2) width 1) (- 1 height)))

(defclass ball  () () movable-block)

(defmethod ball :draw ()
	(color (+ color 128))
	(let ((cx (+ (first position) (/ width 2) -1 *gx*))
	      (cy (+ (second position) (/ height 2) -1 *gy*))
	      (fstep (/ 3.14159 18))
	      (radius (1- (/ (min width height) 2))))
	     (move (+ cx radius) cy)
	     (dotimes (i 36)
	              (draw (truncate (+ cx (* radius (cos (* (1+ i) fstep)))))
		      	    (truncate (+ cy (* radius (sin (* (1+ i) fstep)))))))))

(defclass hand  (name position grasping))

(defmethod hand :top-location  () position)

(defmethod hand :draw ()
	(color (if grasping 143 136))
	(move (+ *gx* -7 (first position)) (+ *gy* (second position)))
	(drawrel 5 0 0 10 5 0 0 -10 5 0 0 20 -15 0 0 -20))

(defmethod hand :new-position (newpos)
	(send self :draw)
	(setf position newpos)
	(send self :draw))

; define all the individual blocks

(setf *blocks*
      (list
        (send table-block :new :name 'table :width 430 :height 10 
			       :position '(0 0) :color 7)
	(send brick :new :name 'b1 :width 40 :height 40 
			       :position '(0 10) :color 1)
	(send brick :new :name 'b2 :width 40 :height 40 
			       :position '(40 10) :color 2)
	(send brick :new :name 'b3 :width 80 :height 80 
			       :position '(80 10) :color 3)
	(send brick :new :name 'b4 :width 40 :height 40 
			       :position '(160 10) :color 4)
	(send wedge :new :name 'w5 :width 40 :height 80 
			       :position '(200 10) :color 5)
	(send brick :new :name 'b6 :width 80 :height 40 
			       :position '(240 10) :color 6)
	(send wedge :new :name 'w7 :width 40 :height 40 
			       :position '(320 10) :color 9)
	(send ball  :new :name 'l8 :width 40 :height 40 
			       :position '(360 10) :color 10)
	(send brick :new :name 'b9 :width 30 :height 30 
			       :position '(400 10) :color 12)
       ))

(dolist (l *blocks*) (set (send l :name) l))

(dolist (l (rest *blocks*)) ; all blocks but the table
	(setf (send table :support-for) 
	      (cons l (send table :support-for))
	      (send l :supported-by)
	      table))

(definst hand *hand* :name '*hand* :position '(0 120))

(defun display-blocks ()
	(cls)
	(dolist (l *blocks*) (send l :drawname)(send l :draw))
	(send *hand* :draw)
	(bottom)
	t)

(defmethod basic-block :put-on (support) ; default case is bad
	(format t
		"Sorry, the ~a cannot be moved.~%"
		name))

(defmethod movable-block :put-on (support)
	(if (send self :get-space support)
	    (and (send *hand* :grasp self)
	    	 (send *hand* :move  self support)
		 (send *hand* :ungrasp self))
	    (format t
	    	    "Sorry, there is no room for ~a on ~a.~%"
		    name
		    (send support :name))))

(defmethod movable-block :get-space (support)
	(or (send self :find-space support)
	    (send self :make-space support)))

(defmethod hand :grasp (obj)
	(unless (eq grasping obj)
		(when (send obj :support-for)
		      (send obj :clear-top))
		(when grasping
		      (send grasping :rid-of))
		(let ((lift (max-height self obj)))
		     (send self :new-position lift)
		     (pause *delay-time*)
		     (send self :new-position 
		     	(list (first (send obj :top-location)) (second lift)))
		     (pause *delay-time*)
		     (send self :new-position (send obj :top-location))
		     (pause *delay-time*))
		(send self :draw)
		(setf grasping obj)
		(send self :draw))
	t)

(defmethod hand :ungrasp (obj)
	(when (send obj :supported-by)
	      (send self :draw)
	      (setf grasping nil)
	      (send self :draw)
	      t))


(defmethod movable-block :rid-of ()
	(send self :put-on table))

(defmethod movable-block :make-space (support)
	(dolist (obstruction (send support :support-for))
		(send obstruction :rid-of)
		(let ((space (send self :find-space support)))
		     (when space (return space)))))

(defmethod  load-bearing-block :clear-top ()
	(dolist (obstacle support-for) (send obstacle :rid-of))
	t)


(defmethod hand :move (obj support)
	(send obj :remove-support)
	(let ((newplace (send obj :get-space support)))
          (let ((lift (max-height obj support)))
	     (send obj :new-position lift)
	     (send self :new-position (send obj :top-location))
	     (pause *delay-time*)
	     (send obj :new-position (list (first newplace) (second lift)))
     	     (send self :new-position (send obj :top-location))
	     (pause *delay-time*)
	     (send obj :new-position newplace)
	     (send self :new-position (send obj :top-location))
	     (pause *delay-time*)))
	(send support :add-support obj)
	t)


; helper function to find height necessary to move object

(defun max-height (obj1 obj2)
	(let    ((source (first (send obj1 :top-location)))
	         (dest   (first (send obj2 :top-location))))
	(let	((roof 0) (min (min source dest)) (max (max source dest)) )
		(dolist (obstacle *blocks*)
			(let ((x (send obstacle :top-location)))
			     (when (and (>= (first x) min)
			     		(<= (first x) max)
					(> (second x) roof))
				   (setf roof (second x)))))
		(list (first (send obj1 :position)) (+ 20 roof)))))
				   
#+:times (defun pause (time) 
	   (let ((fintime (+ (* time internal-time-units-per-second)
			     (get-internal-run-time))))
		(loop (when (> (get-internal-run-time) fintime)
			    (return-from pause)))))
#-:times (defun pause () (dotimes (x (* time 1000))))


; remove-support-for is defined twice, for each load bearing class

(defmethod load-bearing-block :remove-support-for (obj)
	(setf support-for (remove obj support-for))
	t)

(defmethod table-block :remove-support-for (obj)
	(setf support-for (remove obj support-for))
	t)

(defmethod movable-block :remove-support ()
	(when supported-by
	      (send supported-by :remove-support-for self)
	      (setf supported-by nil))
	t)



(defmethod load-bearing-block :add-support (obj)
	(setf support-for 
	      (cons obj support-for)
	      (send obj :supported-by) 
	      self)
	t)

(defmethod table-block :add-support (obj)
	(setf support-for 
	      (cons obj support-for)
	      (send obj :supported-by) 
	      self)
	t)

(defmethod basic-block :add-support (obj)
	t)

(defmethod movable-block :find-space (support)
	(do     ((offset (- (send support :width) width)
	                 (- offset *step-size*)))
		((< offset 0))
		 (unless (intersections-p self offset
		 			  (first (send support :position))
					  (send support :support-for))
			 (return (list (+ offset (first (send support 
			 				      :position)))
				       (+ (second (send support :position))
				          (send support :height)))))))

(defun intersections-p (obj offset base obstacles)
	(dolist (obstacle obstacles)
		(let* ((ls-proposed (+ offset base))
			(rs-proposed (+ ls-proposed (send obj :width)))
			(ls-obstacle (first (send obstacle :position)))
			(rs-obstacle (+ ls-obstacle (send obstacle :width))))
		      (unless (or (>= ls-proposed rs-obstacle)
		      		  (<= rs-proposed ls-obstacle))
			      (return t)))))


(defun m (a b) (send a :put-on b) (bottom))
(defun d () (display-blocks))
(gmodeVGA)
(d)