Browse Source

Mostly working buy/sell system

spesk 1 year ago
parent
commit
713c109db4
1 changed files with 60 additions and 25 deletions
  1. 60 25
      game.lisp

+ 60 - 25
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