engine.lisp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ;;;; Copyright © 2023, Jaidyn Ann <jadedctrl@posteo.at>
  2. ;;;;
  3. ;;;; This program is free software: you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU General Public License as
  5. ;;;; published by the Free Software Foundation, either version 3 of
  6. ;;;; the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This program is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;;;; GNU General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU General Public License
  14. ;;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;;; FLORA-SEARCH-AURORA.ENGINE ⚙
  16. ;;;; The core of the game’s engine, the loop. Not much to see here other
  17. ;;;; than a loop. Honest!
  18. (in-package :flora-search-aurora.engine)
  19. (defun state-loop
  20. (states &key (last-matrix (✎:make-screen-matrix)) (matrix (✎:make-screen-matrix)) (state-params nil))
  21. "Begin the game’s loop, which executes (henceforthly called) state-functions over and over again
  22. until Hell freezes over and a new king reigns supreme.
  23. Given a list of state-functions, STATES, it will execute the first function.
  24. Each state-function must take at least a single parameter, a matrix of characters. A state-function
  25. should edit this matrix in-place, replacing its elements with characters that will later be printed
  26. to the terminal.
  27. What the state-function returns is pretty important, having different repercussions:
  28. * NIL — The function is removed from STATES, and so the next function in STATES will start
  29. getting executed instead.
  30. * T — The function will continue to be run with the same parameters.
  31. * (:FUNCTION FUNCTION :PARAMETERS LIST :DROP NUMBER)
  32. Make note to add a delay w SLEEP to your state functions, or… well, y’know. Your computer will
  33. overheat, or something ¯\_(ツ)_/¯"
  34. (when states
  35. (let ((state-result
  36. (apply (car states) (cons matrix state-params)))) ;; Run the last-added state-loop.
  37. (✎:print-screen-matrix (✎:matrix-delta last-matrix matrix)) ;; Print its results.
  38. ;; (format *error-output* "S::~S~%D::~S~%" states state-result)
  39. (force-output)
  40. (state-loop
  41. (cond ((listp state-result)
  42. (nconc (if (getf state-result :function)
  43. (list (getf state-result :function)))
  44. (nthcdr (or (getf state-result :drop) 0) states)))
  45. ((not state-result)
  46. (cdr states))
  47. ('t states))
  48. :state-params
  49. (cond ((listp state-result)
  50. (getf state-result :parameters))
  51. ((not state-result)
  52. nil)
  53. ('t state-params))
  54. :last-matrix matrix))))
  55. (defun main (states)
  56. "A toplevel-worthy function that configures rendering and kicks off the
  57. game’s loop. Let’s get started!"
  58. (cl-charms:with-curses ()
  59. (cl-charms:enable-raw-input :interpret-control-characters 't)
  60. (✎:hide-cursor)
  61. (✎:clear-screen)
  62. (state-loop states)))