summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEl-BG-1970 <elouangros@hotmail.com>2022-07-27 21:46:26 +0200
committerEl-BG-1970 <elouangros@hotmail.com>2022-07-27 21:46:26 +0200
commita717308dbcbca5f9c6411cd65d7f181531e0aff9 (patch)
treed4f9d74194a78f0a093a15fcceaaf31824c7b77c
parentad07161ab7558d72cc455530e39c3bebfd1a96b7 (diff)
downloadtransacc-a717308dbcbca5f9c6411cd65d7f181531e0aff9.tar.gz
defined inventory methods
defined find-item, delete-item, add-item and remove-item
-rw-r--r--src/player.lisp46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/player.lisp b/src/player.lisp
index f6ee371..6bcf173 100644
--- a/src/player.lisp
+++ b/src/player.lisp
@@ -13,6 +13,52 @@
(items :initform '()
:accessor items)))
+(defmethod find-item ((inv inventory) name)
+ (with-slots (items) inv
+ (find-if
+ #'(lambda (i)
+ (equal (commodities:name (car i)) name))
+ items)))
+
+(defmethod delete-item ((inv inventory) item)
+ ;; removes <item> from inventory list and substracts
+ ;; its stored quantity from the filled attribute
+ (with-slots (filled items) inv
+ (setq items (remove item items))
+ (setq filled (- filled (cdr item)))))
+
+(defmethod add-item ((inv inventory) item quantity)
+ ;; add <quantity> of <item> to inventory
+ (with-slots (size filled items) inv
+ (when (> size (+ filled quantity))
+ (let* ((old-item
+ (find-item inv (commodities:name item)))
+ (qt (if old-item
+ (progn
+ (delete-item inv old-item)
+ (+ quantity (cdr old-item)))
+ quantity)))
+ (prog1
+ (setq filled (+ filled qt))
+ (push (cons item qt)
+ items))))))
+
+(defmethod remove-item ((inv inventory) name quantity)
+ ;; remove <quantity> of <item> from inventory
+ ;; quantity must be positive or zero
+ (assert (>= quantity 0))
+ (with-slots (filled items) inv
+ (let ((item (find-item inv name)))
+ (when item
+ (delete-item inv item)
+ (if (< quantity (cdr item))
+ (progn
+ (add-item inv
+ (car item)
+ (- (cdr item) quantity))
+ (cons (car item) quantity))
+ item)))))
+
(defclass player ()
((name :initarg :name
:reader name)