combat.lisp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. ;; Bizarre Core Assumptions TODO?
  10. ;; * Whenever we think of engaging an enemy, the actual enemy object
  11. ;; is always whatever the first element of the (enemy-ships *sector*)
  12. ;; list. This is done because I'm lazy and it's fairly easy, will
  13. ;; figure out a different way if/when it's needed, but in essence,
  14. ;; the player typically does not make a choice about the characteristics
  15. ;; of the enemy they fight, the game chooses for you. If this is true,
  16. ;; this method is fine.
  17. ;; Combat options:
  18. ;; Attack (with specific weapon)
  19. ;; Regen shields
  20. ;; - (add value to rep-shield-val at cost of overcharing
  21. ;; - (overcharing can cause warp field to go into low-power)
  22. ;; - (overcharging can cause hull damage)
  23. ;; Overcharge gun at cost of overcharing reactor
  24. ;; Attempt to flee into the warp
  25. ;; - (requires full power / overcharged reactor)
  26. ;; - (can also cause reactor to go low power, which removes ability to regen/overcharge)
  27. ;; TODO - Combat always pops off first enemy from the enemy-ships list
  28. (defun combat-loop (sector)
  29. "The main loop responsbile for resolving combat situations"
  30. (let ((combat-state T))
  31. (loop
  32. while combat-state
  33. do (progn
  34. (format T *combat-menu-options-display*)
  35. (let ((top-level-selection (prompt-read "Enter a selection: ")))
  36. (case (read-from-string top-level-selection)
  37. (1 (progn ;; There is a way to collapse this progn into a single
  38. ;; function call, but it's arguably a lot less readable
  39. (let* ((weapon (weapon-select-menu (player-ship-obj sector)))
  40. (attack-result (player-ship-attack
  41. (player-ship-obj sector)
  42. (first (enemy-ships sector))
  43. weapon)))
  44. (if attack-result
  45. (progn
  46. (setf (enemy-ships sector) (delete attack-result (enemy-ships sector)))
  47. (format T "Your opponent has been vanquished!~%")
  48. (top-level-game-menu))))
  49. (enemy-turn sector)))))))))
  50. ;; IN PROGRESS
  51. ;; (defun enemy-turn (sector)
  52. ;; "Perform AI actions based on AI-type associated with enemy ship"
  53. ;; (flet ((enemy-attack (player-ship-obj enemy-obj)
  54. ;; ;; For now, just use the first weapon
  55. ;; (let ((weapon-obj ((first (weapons enemy-obj))))
  56. ;; (ammo
  57. ;; (case (ai-type (first (enemy-ships sector)))
  58. ;; (aggressive (progn
  59. ;; ;; This AI will just stupidly attack until it's out of ammo
  60. ;; ;; If it runs out of ammo, it will try and flee
  61. ;; ))
  62. ;; (fearful () ;; This AI will basically just try to escape, unless it fails
  63. ;; ;; twice in a row, in which it will try to attack
  64. ;; )
  65. ;; (unpredicatable ())))
  66. ;; Select weapon
  67. ;; Determine if hit (% chance?)
  68. ;; If hit resolve shield
  69. ;; -- IF Shields down (ie shield damage causes target shield value to be 0)
  70. ;; -- Apply hull damage
  71. ;; ELSE
  72. ;; -- Decrement shield value per value of shield dmg
  73. (defun weapon-select-menu (player-ship-obj)
  74. (let ((weapon-list (loop for weapon in (weapons player-ship-obj)
  75. for i from 0
  76. collect (list i
  77. (name weapon)
  78. (shield-dmg weapon)
  79. (hull-dmg weapon)
  80. (ammo-cost weapon)))))
  81. (format T "~%Available Ammo: ~A~%~%" (ammo (inventory player-ship-obj)))
  82. (format T "Available Weapons: ~%")
  83. (format-table T weapon-list :column-label '("Number" "Name" "Shield Damage" "Hull Damage" "Ammo Cost"))
  84. (let ((selection (prompt-read "Select a weapon to via the number column: ")))
  85. (format t "Selection was number: ~A and weapon name: ~A~%~%"
  86. selection
  87. (second (nth (parse-integer selection) weapon-list)))
  88. (return-from weapon-select-menu (nth (parse-integer selection) (weapons player-ship-obj))))))
  89. (defun calculate-shield-damage (attacker-obj target-obj attacker-weapon)
  90. "Given a target and a weapon, return a value that the
  91. targets shield value should now be set as"
  92. (let ((new-shield-value (- (rep-shield-val target-obj) (shield-dmg attacker-weapon))))
  93. (setf (ammo (inventory attacker-obj)) (- (ammo (inventory attacker-obj)) (ammo-cost attacker-weapon)))
  94. (if (>= 0 new-shield-value)
  95. (return-from calculate-shield-damage 0)
  96. (return-from calculate-shield-damage new-shield-value))))
  97. (defun calculate-hull-damage (attacker-obj target-obj attacker-weapon)
  98. "Given a target and a weapon, return a value that the
  99. targets hull value should now be set as"
  100. (let ((new-hull-value (- (armor-val target-obj) (hull-dmg attacker-weapon))))
  101. (setf (ammo (inventory attacker-obj)) (- (ammo (inventory attacker-obj)) (ammo-cost attacker-weapon)))
  102. (if (>= 0 new-hull-value)
  103. (return-from calculate-hull-damage 0)
  104. (return-from calculate-hull-damage new-hull-value))))
  105. (defun player-ship-attack (player-ship-obj target-obj attacker-weapon)
  106. "Given a target and the attackers chosen weapon resolve the combat by
  107. calculating shield/hull damage and resolving resources. If the target
  108. is reduced to 0 hull points, return the target-obj and end combat"
  109. (if (> (ammo-cost attacker-weapon) (ammo (inventory player-ship-obj)))
  110. (progn
  111. (format T "You don't have enough ammo to fire your ~A gun!~%" (name attacker-weapon))
  112. (return-from player-ship-attack NIL)))
  113. (let ((calculated-shield-damage (calculate-shield-damage player-ship-obj target-obj attacker-weapon))
  114. (calculated-hull-damage (calculate-hull-damage player-ship-obj target-obj attacker-weapon)))
  115. (setf (rep-shield-val target-obj) calculated-shield-damage) ;; Resolve shield hit
  116. (if (> (rep-shield-val target-obj) 0)
  117. (format T "You hit their repulsor shields for ~A !~%" (shield-dmg attacker-weapon)))
  118. (if (= 0 calculated-shield-damage)
  119. (progn
  120. (format T "Their shields are down!~%")
  121. (setf (armor-val target-obj) calculated-hull-damage)
  122. (format T "You hit their hull for ~A !~%" (hull-dmg attacker-weapon))))
  123. (if (= 0 calculated-hull-damage)
  124. (progn
  125. (format T "Their hulls been breached! You've won the exchange!~%")
  126. (return-from player-ship-attack target-obj))
  127. NIL)))
  128. (defun display-weapons (player-ship-obj)
  129. "Given player ship, display weapons and associated attributes"
  130. (let* ((weapons (weapons player-ship-obj))
  131. (weapon-list (loop for weapon in weapons
  132. collect (list (name weapon)
  133. (shield-dmg weapon)
  134. (hull-dmg weapon)
  135. (ammo-cost weapon)))))
  136. (format T "~%WEAPON DETAILS~%")
  137. (format-table T weapon-list :column-label '("Name" "Shield Damage" "Hull Damage" "Ammo Cost"))))