summaryrefslogtreecommitdiffstats
path: root/src/player.lisp
blob: c9033ac3180b4fba7eda3321a589b57ecf165fb1 (plain)
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
(defpackage player
  (:use :cl)
  (:export :init-player
		   :name
		   :get-cash
		   :get-debt
		   :repay-debt
		   :buy-item
		   :sell-item
		   :find-item
		   :inventory-stats))

(in-package :player)


(defclass player ()
  ((name :initarg :name
		 :reader name)
   (cash  :initarg :cash
		  :accessor cash
		  :reader get-cash)
   (debt  :initarg :debt
		  :accessor debt
		  :reader get-debt)
   (hp   :initform 100
		 :accessor hp)
   (clout :initform 0
		  :accessor clout)
   (stash :initform 0
		  :accessor stash)
   (stock :initform (inventory:new-inventory 100)
		  :accessor stock)))

(defun init-player (name &optional (cash 2000) (debt 5000))
  (make-instance 'player
				 :name name
				 :cash cash
				 :debt debt))

(defmethod buy-item ((p player) item quantity)
  (with-slots (stock cash) p
	(let ((cost (* quantity
				   (commodities:price item))))
	  (when (and (>= cash cost)
				 (inventory:add-item stock item quantity))
		(setq cash (- cash cost))
		t))))

(defmethod sell-item ((p player) item amount)
  (with-slots (stock cash) p
	(let ((stocked-item
		   (inventory:remove-item stock
								  (commodities:name item) amount)))
	  (when stocked-item
	    (setq cash
			  (+ cash (* (cdr stocked-item)
						 (commodities:price item))))))))

(defmethod find-item ((p player) name)
  (with-slots (stock) p
	  (inventory:find-item stock name)))

(defmethod repay-debt ((p player) amount)
  (with-slots (debt cash) p
	(when (and (<= amount debt)
			   (<= amount cash))
	  (setq cash (- cash amount)
			debt (- debt amount))
	  t)))

(defmethod inventory-stats ((p player))
  (inventory:how-filled (stock p)))