713c109db44f5f2461ecf9b212871f80d3c31639
commit 713c109db44f5f2461ecf9b212871f80d3c31639
Author: spesk <spw01@protonmail.com>
Date: Tue Feb 7 22:23:30 2023 -0500

Mostly working buy/sell system

diff --git a/game.lisp b/game.lisp
index f351889..c8f59a5 100644
--- a/game.lisp
+++ b/game.lisp
@@ -111,21 +111,6 @@ Actions:
4 | Return to top level | r
")

-(defparameter *resource-opt-lookup* (list (cons 'pf "petrofuel")
- (cons 'petrofuel "petrofuel")
- (cons 'gr "gruel")
- (cons 'gruel "gruel")
- (cons 'sp "spice")
- (cons 'spice "spice")
- (cons 'am "ammo")
- (cons 'ammo "ammo")
- (cons 'ar "archeotech")
- (cons 'archeotech "archeotech")))
-
-(defun handle-str-lookup (input-str lookup-table)
- (let ((handler (cdr (assoc input-str lookup-table))))
- (if handler (return-from handle-str-lookup handler) (format t "Invalid resource~%~%"))))
-
(defun buy-transaction (resource quantity)
"Do they actual purchase transaction, not intended to be called interactively"
(let* ((available-player-funds (player-ship-credits *player-ship*))
@@ -147,35 +132,85 @@ Actions:
(case resource-sym
;;; This is insanely annoying, and will need to be duplicated
;;; for the sell logic, but don't know how else to handle this here
- ('gruel (progn
+ (gruel (progn
(funcall minus-funds total-cost)
(setf (player-inventory-gruel inventory)
(+ (player-inventory-gruel inventory) quantity))))
- ('archeotech (progn
+ (archeotech (progn
(funcall minus-funds total-cost)
(setf (player-inventory-archeotech inventory)
(+ (player-inventory-archeotech inventory) quantity))))
- ('petrofuel (progn
+ (petrofuel (progn
(funcall minus-funds total-cost)
(setf (player-inventory-petrofuel inventory)
(+ (player-inventory-petrofuel inventory) quantity))))
- ('spice (progn
+ (spice (progn
(funcall minus-funds total-cost)
(setf (player-inventory-spice inventory)
(+ (player-inventory-spice inventory) quantity))))
- ('ammo (progn
+ (ammo (progn
(funcall minus-funds total-cost)
(setf (player-inventory-ammo inventory)
- (+ (player-inventory-ammo inventory) quantity))))
- (otherwise (format T "Invalid"))))))))
+ (+ (player-inventory-ammo inventory) quantity))))))
+ (format T "Successfully purchased ~A ~A~%" quantity resource)))))

(defun buy-menu ()
(let ((item-to-buy (prompt-read "Enter a resource to buy: "))
- (quantity (prompt-read "Enter a quantity to buy: ")))
- (handle-str-lookup (read-from-string item-to-buy) *resource-opt-lookup*)))
+ (quantity (parse-integer (prompt-read "Enter a quantity to buy: "))))
+ (if (member item-to-buy '("gruel" "ammo" "petrofuel" "archeotech" "spice") :test #'string=)
+ (progn
+ (buy-transaction item-to-buy quantity)
+ (trade-menu))))
+
+
+(defun sell-transaction (resource quantity)
+ "Do the sale transaction, not intended to be called interactively"
+ (let* ((available-player-funds (player-ship-credits *player-ship*))
+ (inventory (player-ship-inventory *player-ship*))
+ (available-player-resource (funcall (symbol-function (find-symbol (string-upcase
+ (concatenate 'string "player-inventory-" resource))))
+ inventory))
+ (price (funcall (symbol-function (find-symbol (string-upcase
+ (concatenate 'string "market-price-of-" resource))))
+ (sector-market *sector*)))
+ (total-profit (* quantity price)))
+ (if (> quantity available-player-resource)
+ (progn
+ (format T "Not enough ~A to sell ~A. You have ~A~%" resource quantity available-player-resource)
+ (return-from sell-transaction NIL))
+ (progn
+ (let ((resource-sym (read-from-string resource))
+ (remove-resource (lambda (amount)
+ (let ((new-credits (+ available-player-funds total-profit)))
+ (setf (player-ship-credits *player-ship*) new-credits))
+ (- available-player-resource amount)))) ; This is pretty convoluted
+ ;;; remove-resource lambda is a pretty bad idea
+ ;;; it is used to set the new credits amount and then return the amount needed to
+ ;;; be removed from the resource in the player inventory. I did it this way
+ ;;; to keep the logic concise, but it smells bad and is probably stupid
+ (case resource-sym
+ ;;; This is insanely annoying, and will need to be duplicated
+ ;;; for the sell logic, but don't know how else to handle this here
+ (gruel (progn
+ (setf (player-inventory-gruel inventory) (funcall remove-resource quantity))))
+ (petrofuel (progn
+ (setf (player-inventory-petrofuel inventory) (funcall remove-resource quantity))))
+ (spice (progn
+ (setf (player-inventory-spice inventory) (funcall remove-resource quantity))))
+ (ammo (progn
+ (setf (player-inventory-ammo inventory) (funcall remove-resource quantity))))
+ (archeotech (progn
+ (setf (player-inventory-archeotech inventory) (funcall remove-resource quantity))))))
+ (format T "Successfully sold ~A ~A~%" quantity resource)))))

(defun sell-menu ()
- (format T "Called sell menu~%"))
+ (let ((item-to-sell (prompt-read "Enter a resource to sell: "))
+ (quantity (parse-integer (prompt-read "Enter a quantity to sell: "))))
+ (if (member item-to-sell '("gruel" "ammo" "petrofuel" "archeotech" "spice") :test #'string=)
+ (progn
+ (sell-transaction item-to-sell quantity)
+ (trade-menu))))
+

(defun display-prices ()
;;; (funcall (symbol-function (find-symbol (string-upcase (concatenate 'string "market-price-of-" item)))) (sector-market *sector*)) ;;; A call by string reference method for function calls