combat.lisp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. (defun enemy-turn (sector)
  51. "Perform AI actions based on AI-type associated with enemy ship"
  52. (case (ai-type (first (enemy-ships sector)))
  53. (aggressive (progn
  54. ;; This AI will just stupidly attack until it's out of ammo
  55. ;; If it runs out of ammo, it will try and flee
  56. ))
  57. (fearful ())
  58. (unpredicatable ())))
  59. ;; Select weapon
  60. ;; Determine if hit (% chance?)
  61. ;; If hit resolve shield
  62. ;; -- IF Shields down (ie shield damage causes target shield value to be 0)
  63. ;; -- Apply hull damage
  64. ;; ELSE
  65. ;; -- Decrement shield value per value of shield dmg
  66. (defun weapon-select-menu (player-ship-obj)
  67. (let ((weapon-list (loop for weapon in (weapons player-ship-obj)
  68. for i from 0
  69. collect (list i
  70. (name weapon)
  71. (shield-dmg weapon)
  72. (hull-dmg weapon)
  73. (ammo-cost weapon)))))
  74. (format T "~%Available Ammo: ~A~%~%" (ammo (inventory player-ship-obj)))
  75. (format T "Available Weapons: ~%")
  76. (format-table T weapon-list :column-label '("Number" "Name" "Shield Damage" "Hull Damage" "Ammo Cost"))
  77. (let ((selection (prompt-read "Select a weapon to via the number column: ")))
  78. (format t "Selection was number: ~A and weapon name: ~A~%~%"
  79. selection
  80. (second (nth (parse-integer selection) weapon-list)))
  81. (return-from weapon-select-menu (nth (parse-integer selection) (weapons player-ship-obj))))))
  82. (defun calculate-shield-damage (attacker-obj target-obj attacker-weapon)
  83. "Given a target and a weapon, return a value that the
  84. targets shield value should now be set as"
  85. (let ((new-shield-value (- (rep-shield-val target-obj) (shield-dmg attacker-weapon))))
  86. (setf (ammo (inventory attacker-obj)) (- (ammo (inventory attacker-obj)) (ammo-cost attacker-weapon)))
  87. (if (>= 0 new-shield-value)
  88. (return-from calculate-shield-damage 0)
  89. (return-from calculate-shield-damage new-shield-value))))
  90. (defun calculate-hull-damage (attacker-obj target-obj attacker-weapon)
  91. "Given a target and a weapon, return a value that the
  92. targets hull value should now be set as"
  93. (let ((new-hull-value (- (armor-val target-obj) (hull-dmg attacker-weapon))))
  94. (setf (ammo (inventory attacker-obj)) (- (ammo (inventory attacker-obj)) (ammo-cost attacker-weapon)))
  95. (if (>= 0 new-hull-value)
  96. (return-from calculate-hull-damage 0)
  97. (return-from calculate-hull-damage new-hull-value))))
  98. (defun player-ship-attack (player-ship-obj target-obj attacker-weapon)
  99. "Given a target and the attackers chosen weapon resolve the combat by
  100. calculating shield/hull damage and resolving resources. If the target
  101. is reduced to 0 hull points, return the target-obj and end combat"
  102. (if (> (ammo-cost attacker-weapon) (ammo (inventory player-ship-obj)))
  103. (progn
  104. (format T "You don't have enough ammo to fire your ~A gun!~%" (name attacker-weapon))
  105. (return-from player-ship-attack NIL)))
  106. (let ((calculated-shield-damage (calculate-shield-damage player-ship-obj target-obj attacker-weapon))
  107. (calculated-hull-damage (calculate-hull-damage player-ship-obj target-obj attacker-weapon)))
  108. (setf (rep-shield-val target-obj) calculated-shield-damage) ;; Resolve shield hit
  109. (if (> (rep-shield-val target-obj) 0)
  110. (format T "You hit their repulsor shields for ~A !~%" (shield-dmg attacker-weapon)))
  111. (if (= 0 calculated-shield-damage)
  112. (progn
  113. (format T "Their shields are down!~%")
  114. (setf (armor-val target-obj) calculated-hull-damage)
  115. (format T "You hit their hull for ~A !~%" (hull-dmg attacker-weapon))))
  116. (if (= 0 calculated-hull-damage)
  117. (progn
  118. (format T "Their hulls been breached! You've won the exchange!~%")
  119. (return-from player-ship-attack target-obj))
  120. NIL)))
  121. (defun display-weapons (player-ship-obj)
  122. "Given player ship, display weapons and associated attributes"
  123. (let* ((weapons (weapons player-ship-obj))
  124. (weapon-list (loop for weapon in weapons
  125. collect (list (name weapon)
  126. (shield-dmg weapon)
  127. (hull-dmg weapon)
  128. (ammo-cost weapon)))))
  129. (format T "~%WEAPON DETAILS~%")
  130. (format-table T weapon-list :column-label '("Name" "Shield Damage" "Hull Damage" "Ammo Cost"))))