economy.lisp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. ;;; TRADING ;;;
  2. (defparameter *trade-opt-lookup* (list (cons '1 'buy-menu)
  3. (cons 'b 'buy-menu)
  4. (cons '2 'sell-menu)
  5. (cons 's 'sell-menu)
  6. (cons '3 'top-level-game-menu)
  7. (cons 'r 'top-level-game-menu)))
  8. (defvar *trade-menu-options-display* "
  9. Actions:
  10. 1 | Buy | b
  11. 2 | Sell | s
  12. 3 | Return to top level | r
  13. ")
  14. (defun buy-transaction (resource quantity)
  15. "Do they actual purchase transaction, not intended to be called interactively"
  16. (let* ((available-player-funds (player-ship-credits *player-ship*))
  17. (inventory (player-ship-inventory *player-ship*))
  18. (price (funcall (symbol-function (find-symbol (string-upcase
  19. (concatenate 'string "market-price-of-" resource))))
  20. (sector-market *sector*)))
  21. (total-cost (* quantity price)))
  22. (if (> total-cost available-player-funds)
  23. (progn
  24. (format T "Not enough credits to buy ~A ~A at ~A credits~%" quantity resource price)
  25. (format T "~%PLAYER CREDITS: ~A~%" (player-ship-credits *player-ship*))
  26. (return-from buy-transaction NIL))
  27. (progn
  28. (let ((resource-sym (read-from-string resource))
  29. (minus-funds (lambda (amount)
  30. (let ((remainder (- available-player-funds amount)))
  31. (setf (player-ship-credits *player-ship*) remainder)))))
  32. (case resource-sym
  33. ;;; This is insanely annoying, and will need to be duplicated
  34. ;;; for the sell logic, but don't know how else to handle this here
  35. (gruel (progn
  36. (funcall minus-funds total-cost)
  37. (setf (player-inventory-gruel inventory)
  38. (+ (player-inventory-gruel inventory) quantity))))
  39. (archeotech (progn
  40. (funcall minus-funds total-cost)
  41. (setf (player-inventory-archeotech inventory)
  42. (+ (player-inventory-archeotech inventory) quantity))))
  43. (petrofuel (progn
  44. (funcall minus-funds total-cost)
  45. (setf (player-inventory-petrofuel inventory)
  46. (+ (player-inventory-petrofuel inventory) quantity))))
  47. (spice (progn
  48. (funcall minus-funds total-cost)
  49. (setf (player-inventory-spice inventory)
  50. (+ (player-inventory-spice inventory) quantity))))
  51. (ammo (progn
  52. (funcall minus-funds total-cost)
  53. (setf (player-inventory-ammo inventory)
  54. (+ (player-inventory-ammo inventory) quantity))))))
  55. (format T "Successfully purchased ~A ~A~%" quantity resource)))))
  56. (defun buy-menu ()
  57. (let ((item-to-buy (prompt-read "Enter a resource to buy: "))
  58. (quantity (parse-integer (prompt-read "Enter a quantity to buy: "))))
  59. (if (member item-to-buy '("gruel" "ammo" "petrofuel" "archeotech" "spice") :test #'string=)
  60. (progn
  61. (buy-transaction item-to-buy quantity)
  62. (trade-menu)))))
  63. (defun sell-transaction (resource quantity)
  64. "Do the sale transaction, not intended to be called interactively"
  65. (let* ((available-player-funds (player-ship-credits *player-ship*))
  66. (inventory (player-ship-inventory *player-ship*))
  67. (available-player-resource (funcall (symbol-function (find-symbol (string-upcase
  68. (concatenate 'string "player-inventory-" resource))))
  69. inventory))
  70. (price (funcall (symbol-function (find-symbol (string-upcase
  71. (concatenate 'string "market-price-of-" resource))))
  72. (sector-market *sector*)))
  73. (total-profit (* quantity price)))
  74. (if (> quantity available-player-resource)
  75. (progn
  76. (format T "Not enough ~A to sell ~A. You have ~A~%" resource quantity available-player-resource)
  77. (return-from sell-transaction NIL))
  78. (progn
  79. (let ((resource-sym (read-from-string resource))
  80. (remove-resource (lambda (amount)
  81. (let ((new-credits (+ available-player-funds total-profit)))
  82. (setf (player-ship-credits *player-ship*) new-credits))
  83. (- available-player-resource amount)))) ; This is pretty convoluted
  84. ;;; remove-resource lambda is a pretty bad idea
  85. ;;; it is used to set the new credits amount and then return the amount needed to
  86. ;;; be removed from the resource in the player inventory. I did it this way
  87. ;;; to keep the logic concise, but it smells bad and is probably stupid
  88. (case resource-sym
  89. ;;; This is insanely annoying, and will need to be duplicated
  90. ;;; for the sell logic, but don't know how else to handle this here
  91. (gruel (progn
  92. (setf (player-inventory-gruel inventory) (funcall remove-resource quantity))))
  93. (petrofuel (progn
  94. (setf (player-inventory-petrofuel inventory) (funcall remove-resource quantity))))
  95. (spice (progn
  96. (setf (player-inventory-spice inventory) (funcall remove-resource quantity))))
  97. (ammo (progn
  98. (setf (player-inventory-ammo inventory) (funcall remove-resource quantity))))
  99. (archeotech (progn
  100. (setf (player-inventory-archeotech inventory) (funcall remove-resource quantity))))))
  101. (format T "Successfully sold ~A ~A~%" quantity resource)))))
  102. (defun sell-menu ()
  103. (let ((item-to-sell (prompt-read "Enter a resource to sell: "))
  104. (quantity (parse-integer (prompt-read "Enter a quantity to sell: "))))
  105. (if (member item-to-sell '("gruel" "ammo" "petrofuel" "archeotech" "spice") :test #'string=)
  106. (progn
  107. (sell-transaction item-to-sell quantity)
  108. (trade-menu)))))
  109. ;;; This is kept around in case I need it. I'm not sure
  110. ;;; this is any less 'bad' than how buy/sell-transaction
  111. ;;; currently works
  112. (defmacro dynamic-slot-access (predicate slotname accessor)
  113. "Given a predicate where the predicate is a struct slot accessor like 'market-price-of-',
  114. a slotname like 'petrofuel', and a struct location, return the result of the slot accessor function"
  115. `(funcall ,(symbol-function (find-symbol (string-upcase (concatenate 'string predicate slotname)))) ,accessor))
  116. ;; Can't get this to work how I expect, need to experiment morex
  117. ;; (defmacro dynamic-slot-setting (predicate slotname accessor value)
  118. ;; `(setf (funcall (symbol-function (find-symbol (string-upcase (concatenate 'string predicate slotname)))) accessor) ,value))
  119. (defun display-prices ()
  120. (let ((market-list (list
  121. (list "Petrofuel" (market-price-of-petrofuel (sector-market *sector*)))
  122. (list "Gruel" (market-price-of-gruel (sector-market *sector*)))
  123. (list "Spice" (market-price-of-spice (sector-market *sector*)))
  124. (list "Ammo" (market-price-of-ammo (sector-market *sector*)))
  125. (list "Archeotech" (market-price-of-archeotech (sector-market *sector*))))))
  126. (format T "~%PLAYER CREDITS: ~A~%" (player-ship-credits *player-ship*))
  127. (format T "~%MARKET PRICES~%")
  128. (format-table T market-list :column-label '("Resource" "Cost"))))
  129. (defun trade-menu ()
  130. (display-prices)
  131. (display-inventory)
  132. (format t *trade-menu-options-display*)
  133. (let ((option (prompt-read "Enter an option: ")))
  134. (format t "~%")
  135. (handle-opt (read-from-string option) *trade-opt-lookup*))
  136. (trade-menu))
  137. ;;; END TRADING ;;;
  138. ;;; MARKET ;;;
  139. ;;; Use this parameter when randomizing market prices. Used to lookup how
  140. ;;; 'random' prices should really be."
  141. (defparameter *market-price-bounds*
  142. (list (cons 'petrofuel '(10 41))
  143. (cons 'ammo '(5 31))
  144. (cons 'archeotech '(750 2001))
  145. (cons 'spice '(5 101))
  146. (cons 'gruel '(1 16))))
  147. (defun randomize-market-prices (market)
  148. (let ((get-random-val (lambda (resource-arg)
  149. (+ (cadr resource-arg)
  150. (random (caddr resource-arg))))))
  151. (loop for resource in *market-price-bounds*
  152. do (case (car resource)
  153. (gruel (setf (market-price-of-gruel market) (funcall get-random-val resource)))
  154. (ammo (setf (market-price-of-ammo market) (funcall get-random-val resource)))
  155. (spice (setf (market-price-of-spice market) (funcall get-random-val resource)))
  156. (archeotech (setf (market-price-of-archeotech market) (funcall get-random-val resource)))
  157. (petrofuel (setf (market-price-of-petrofuel market) (funcall get-random-val resource)))))))