about summary refs log tree commit diff stats
path: root/crpg.lisp
blob: 10f40d38ab5954223def99aee87f7f1fa3c4e6db (plain) (blame)
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
#!/usr/local/bin/clisp
(load "cscreen")
(require "cscreen")
(defpackage #:crpg
  (:use #:common-lisp #:cscreen)
  (:export
   #:main))
(in-package #:crpg)
(defclass <location> () ((row :initarg r :accessor row)
			 (col :initarg c :accessor col)))
(defmethod print-object ((obj <location>) stream)
  (format stream "~A ~A" (row obj) (col obj)))

;; TODO: <location-contents> class, or maybe rename the other one to <coord>
(defvar *monster* (make-array '(10 10)))
(defvar *item* (make-array '(10 10)))

(defvar *curr-loc* (make-instance (find-class '<location>) 'r 5 'c 5))
(defconstant +move-err+ "You cannot move in that direction!")
(defconstant +press-key+ "Press Enter to continue.")
(defconstant +exit+ (make-instance (find-class '<location>)
				   'r (+ (random 10) 1)
				   'c (+ (random 10) 1)))
;; (format *standard-output* "~A~%" +exit+)
;; (sleep 3)

;; TODO: <player> class?
(defvar *gold* 0)
(defvar *health* 20)
(defvar *weapon* 1)

(defclass <monster> () ((name :initarg n :reader name)
			(attack :initarg a :reader attack)
			(health :initarg h :accessor health)))
(defmethod print-object ((obj <monster>) stream)
  (format stream "~A" (name obj)))
(defun create-monster (n a h)
  (make-instance (find-class '<monster>) 'n n 'a a 'h h))
(defconstant +monsters+
  (vector
   (create-monster "blind bat" 2 1)
   (create-monster "rat" 1 1)
   (create-monster "snake" 3 1)
   (create-monster "goblin" 2 3)
   (create-monster "troll" 4 4)
   (create-monster "bear" 5 5)
   (create-monster "lion" 5 4)
   (create-monster "sabretooth" 6 5)
   (create-monster "elephant" 7 8)
   (create-monster "dragon" 9 8)))
(defclass <item> () ((name :initarg n :reader name)
		     (class :initarg c :reader class)
		     (val :initarg v :reader val)))
(defun create-item (n c v)
  (make-instance (find-class '<item>) 'n n 'c c 'v v))
(defconstant +items+
  (vector
   (create-item "apple" 'food 1)
   (create-item "bread" 'food 2)
   (create-item "chicken" 'food 3)))
(defgeneric copy (obj))
(defmethod copy ((obj <monster>))
  (create-monster (name obj) (attack obj) (health obj)))
(defvar *curr-monster*)
(defvar *monster-gold*)
(defun my-print (x)
  (format *standard-output* "~A~%" x))
(defun print-help ()
  (cursor 14 1)
  (my-print "Commands:   n - north")
  (my-print "            s - south")
  (my-print "            e - east")
  (my-print "            w - west")
  (my-print "            a - attack")
  (my-print "            x - use exit")
  (write-char #\newline)
  (my-print "            q - quit"))
(defgeneric move (loc dir))
(defmethod move ((loc <location>) dir)
  (case dir
	((n) (if (= (row loc) 1)
		 (my-print +move-err+)
	       (progn (setf (row loc) (- (row loc) 1))
		      (my-print "You go north."))))
	((s) (if (= (row loc) 10)
		 (my-print +move-err+)
	       (progn (setf (row loc) (+ (row loc) 1))
		      (my-print "You go south."))))
	((w) (if (= (col loc) 1)
		 (my-print +move-err+)
	       (progn (setf (col loc) (- (col loc) 1))
		      (my-print "You go west."))))
	((e) (if (= (col loc) 10)
		 (my-print +move-err+)
	       (progn (setf (col loc) (+ (col loc) 1))
		      (my-print "You go east."))))
	(t (error "bad dir"))))
(defgeneric loc= (l r))
(defmethod loc= ((l <location>) (r <location>))
  (and (= (row l) (row r)) (= (col l) (col r))))
(defun handle-cmd (cmd)
  (cond
   ((string= cmd "n")
    (move *curr-loc* 'n))
   ((string= cmd "s")
    (move *curr-loc* 's))
   ((string= cmd "w")
    (move *curr-loc* 'w))
   ((string= cmd "e")
    (move *curr-loc* 'e))
   ((string= cmd "x")
    (if (loc= *curr-loc* +exit+)
	(if (< *gold* 100)
	    (my-print "You dont have enough gold!")
	  (my-print "You have escaped! Well done!"))))
   ((string= cmd "a")
    (if (null *curr-monster*)
	(my-print "There's nothing to attack!")
      (progn (loop while (and (> (health *curr-monster*) 0) (> *health* 0))
		   do (cursor 10 1) (clrtoeol)
		   (cursor 11 1) (clrtoeol)
		   (cursor 12 1) (clrtoeol)
		   (cursor 10 1)
		   (let ((attack (+ *weapon* (random 9)))
			 (monster-attack (attack *curr-monster*)))
		     (cond
		      ((= attack monster-attack)
		       (my-print "No one wins this round."))
		      ((> attack monster-attack)
		       (format *standard-output* "You deal the ~A a blow!~%" (name *curr-monster*))
		       (setf (health *curr-monster*) (- (health *curr-monster*) 1)))
		      ((< attack monster-attack)
		       (my-print "You have been wounded!")
		       (setq *health* (- *health* 1))))
		     (my-print +press-key+)
		     (read-line)))
	     (cursor 10 1)
	     (if (> *health* 0)
		 (progn (my-print "You won the fight!")
			(format *standard-output* "You found ~A pieces of gold!~%" *monster-gold*)
			(setq *gold* (+ *gold* *monster-gold*))
			(setq *curr-monster* nil)
			(setf (aref *monster* (row *curr-loc*) (col *curr-loc*)) nil)
			(my-print +press-key+)
			(read-line))
	       (progn (format *standard-output* "The ~A killed you!~%" (name *curr-monster*))
		      (my-print "Game over!")
		      (throw 'quit nil))))))
   ((string= cmd "q")
    (my-print "Bye!")
    (throw 'quit nil))))
(defun get-next-cmd ()
  (cursor 8 1)
  (my-print "What now")
  (let ((reply (read-line)))
    (cursor 10 1)
    (clrtoeol)
    reply))
(defun main-loop ()
  (loop
   (cursor 1 1)
   (format *standard-output* "Your position: ~A~%" *curr-loc*)
   (cursor 1 25)
   (format *standard-output* "Gold: ~A   Health: ~A~%" *gold* *health*)
   (cursor 2 1)
   (if (loc= *curr-loc* +exit+)
       (my-print "You are at the exit!")
     (clrtoeol))
   (cursor 3 1)
   (clrtoeol)
   (let ((monster-type (aref *monster* (row *curr-loc*) (col *curr-loc*))))
     (if (null monster-type)
	 (progn (setq *curr-monster* nil)
		(setq *monster-gold* 0))
       (progn (setq *curr-monster* (copy (elt +monsters+ monster-type)))
	      (format *standard-output* "Monster: ~A~%" *curr-monster*)
	      (setq *monster-gold* (* monster-type (random 6))))))
   (handle-cmd (get-next-cmd))))
(defun init ()
  (page)
  (setq *random-state* (make-random-state t))
  (do ((row 1 (+ row 1)))
      ((>= row 10))
      (do ((col 1 (+ col 1)))
	  ((>= col 10))
	  (let ((rnd (random 11)))
	    (if (/= rnd 0)
		(setf (aref *monster* row col) (- rnd 1))))))
  
  (do ((row 1 (+ row 1)))
      ))
(defun main ()
  (init)
  (print-help)
  (catch 'quit (main-loop)))
(provide "crpg")
(crpg:main)