combat.lisp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ;;; Combat systems implemented here
  2. (defvar *combat-menu-options-display* "
  3. Actions:
  4. 1 | Attack | a
  5. 2 | Regen. Shields | r
  6. 3 | Overcharge Gun | o
  7. 4 | Attempt to flee | f
  8. ")
  9. ;; Combat options:
  10. ;; Attack (with specific weapon)
  11. ;; Regen shields
  12. ;; - (add value to rep-shield-val at cost of overcharing
  13. ;; - (overcharing can cause warp field to go into low-power)
  14. ;; - (overcharging can cause hull damage)
  15. ;; Overcharge gun at cost of overcharing reactor
  16. ;; Attempt to flee into the warp
  17. ;; - (requires full power / overcharged reactor)
  18. ;; - (can also cause reactor to go low power, which removes ability to regen/overcharge)
  19. (defun combat-loop (sector)
  20. "The main loop responsbile for resolving combat situations"
  21. (let ((combat-state T))
  22. (loop
  23. while combat-state
  24. do (progn
  25. (format T *combat-menu-options-display*)
  26. (let ((top-level-selection (prompt-read "Enter a selection: ")))
  27. (case (read-from-string top-level-selection)
  28. (1 (progn ;; There is a way to collapse this progn into a single
  29. ;; function call, but it's arguably a lot less readable
  30. (let* ((weapon (weapon-select-menu (player-ship-obj sector)))
  31. (attack-result (player-ship-attack
  32. (player-ship-obj sector)
  33. (first (enemy-ships sector))
  34. weapon)))
  35. (if attack-result
  36. (progn
  37. (setf (enemy-ships sector) (delete attack-result (enemy-ships sector)))
  38. (format T "Your opponent has been vanquished!~%")
  39. (top-level-game-menu))))))))))))
  40. ;; Select weapon
  41. ;; Determine if hit (% chance?)
  42. ;; If hit resolve shield
  43. ;; -- IF Shields down (ie shield damage causes target shield value to be 0)
  44. ;; -- Apply hull damage
  45. ;; ELSE
  46. ;; -- Decrement shield value per value of shield dmg
  47. (defun weapon-select-menu (player-ship-obj)
  48. (let ((weapon-list (loop for weapon in (weapons player-ship-obj)
  49. for i from 0
  50. collect (list i
  51. (name weapon)
  52. (shield-dmg weapon)
  53. (hull-dmg weapon)
  54. (ammo-cost weapon)))))
  55. (format T "~%Available Ammo: ~A~%~%" (ammo (inventory player-ship-obj)))
  56. (format T "Available Weapons: ~%")
  57. (format-table T weapon-list :column-label '("Number" "Name" "Shield Damage" "Hull Damage" "Ammo Cost"))
  58. (let ((selection (prompt-read "Select a weapon to via the number column: ")))
  59. (format t "Selection was number: ~A and weapon name: ~A~%~%"
  60. selection
  61. (second (nth (parse-integer selection) weapon-list)))
  62. (return-from weapon-select-menu (nth (parse-integer selection) (weapons player-ship-obj))))))
  63. (defun calculate-shield-damage (attacker-obj target-obj attacker-weapon)
  64. "Given a target and a weapon, return a value that the
  65. targets shield value should now be set as"
  66. (let ((new-shield-value (- (rep-shield-val target-obj) (shield-dmg attacker-weapon))))
  67. (setf (ammo (inventory attacker-obj)) (- (ammo (inventory attacker-obj)) (ammo-cost attacker-weapon)))
  68. (if (>= 0 new-shield-value)
  69. (return-from calculate-shield-damage 0)
  70. (return-from calculate-shield-damage new-shield-value))))
  71. (defun calculate-hull-damage (attacker-obj target-obj attacker-weapon)
  72. "Given a target and a weapon, return a value that the
  73. targets hull value should now be set as"
  74. (let ((new-hull-value (- (armor-val target-obj) (hull-dmg attacker-weapon))))
  75. (setf (ammo (inventory attacker-obj)) (- (ammo (inventory attacker-obj)) (ammo-cost attacker-weapon)))
  76. (if (>= 0 new-hull-value)
  77. (return-from calculate-hull-damage 0)
  78. (return-from calculate-hull-damage new-hull-value))))
  79. (defun player-ship-attack (player-ship-obj target-obj attacker-weapon)
  80. "Given a target and the attackers chosen weapon resolve the combat by
  81. calculating shield/hull damage and resolving resources. If the target
  82. is reduced to 0 hull points, return the target-obj and end combat"
  83. (if (> (ammo-cost attacker-weapon) (ammo (inventory player-ship-obj)))
  84. (progn
  85. (format T "You don't have enough ammo to fire your ~A gun!~%" (name attacker-weapon))
  86. (return-from player-ship-attack NIL)))
  87. (let ((calculated-shield-damage (calculate-shield-damage player-ship-obj target-obj attacker-weapon))
  88. (calculated-hull-damage (calculate-hull-damage player-ship-obj target-obj attacker-weapon)))
  89. (setf (rep-shield-val target-obj) calculated-shield-damage) ;; Resolve shield hit
  90. (format T "You hit their repulsor shields for ~A !~%" (shield-dmg attacker-weapon))
  91. (if (= 0 calculated-shield-damage)
  92. (progn
  93. (setf (armor-val target-obj) calculated-hull-damage)
  94. (format T "You hit their hull for ~A !~%" (hull-dmg attacker-weapon))))
  95. (if (= 0 calculated-hull-damage)
  96. (progn
  97. (format T "Their hulls been breached! You've won the exchange!~%")
  98. (return-from player-ship-attack target-obj))
  99. NIL)))
  100. (defun display-weapons (player-ship-obj)
  101. "Given player ship, display weapons and associated attributes"
  102. (let* ((weapons (weapons player-ship-obj))
  103. (weapon-list (loop for weapon in weapons
  104. collect (list (name weapon)
  105. (shield-dmg weapon)
  106. (hull-dmg weapon)
  107. (ammo-cost weapon)))))
  108. (format T "~%WEAPON DETAILS~%")
  109. (format-table T weapon-list :column-label '("Name" "Shield Damage" "Hull Damage" "Ammo Cost"))))