From 0ca49f895163d7fc77fd49a0c8ffaf042661d283 Mon Sep 17 00:00:00 2001 From: El-BG-1970 Date: Thu, 28 Jul 2022 18:30:52 +0200 Subject: moved inventory out of player.lisp --- src/inventory.lisp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/inventory.lisp (limited to 'src/inventory.lisp') diff --git a/src/inventory.lisp b/src/inventory.lisp new file mode 100644 index 0000000..17c02e7 --- /dev/null +++ b/src/inventory.lisp @@ -0,0 +1,62 @@ +(defpackage inventory + (:use :cl) + (:export :inventory + :add-item + :remove-item)) + +(in-package :inventory) + + +(defclass inventory () + ((size :initarg :size + :reader size) + (filled :initform 0 + :accessor filled) + (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 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 of 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 of 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))))) -- cgit v1.2.3