summaryrefslogtreecommitdiffstats
path: root/src/inventory.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'src/inventory.lisp')
-rw-r--r--src/inventory.lisp58
1 files changed, 32 insertions, 26 deletions
diff --git a/src/inventory.lisp b/src/inventory.lisp
index 17c02e7..429f666 100644
--- a/src/inventory.lisp
+++ b/src/inventory.lisp
@@ -1,6 +1,7 @@
(defpackage inventory
(:use :cl)
- (:export :inventory
+ (:export :new-inventory
+ :filled
:add-item
:remove-item))
@@ -15,6 +16,10 @@
(items :initform '()
:accessor items)))
+(defun new-inventory (size)
+ (when (> size 0)
+ (make-instance 'inventory :size size)))
+
(defmethod find-item ((inv inventory) name)
(with-slots (items) inv
(find-if
@@ -31,32 +36,33 @@
(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))))))
+ (when (> quantity 0)
+ (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)))))
+ (when (> 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))))))