2023-06-09 07:07:28 -05:00
|
|
|
|
;;;; Copyright © 2023, Jaidyn Ann <jadedctrl@posteo.at>
|
|
|
|
|
;;;;
|
|
|
|
|
;;;; This program is free software: you can redistribute it and/or
|
|
|
|
|
;;;; modify it under the terms of the GNU General Public License as
|
|
|
|
|
;;;; published by the Free Software Foundation, either version 3 of
|
|
|
|
|
;;;; the License, or (at your option) any later version.
|
|
|
|
|
;;;;
|
|
|
|
|
;;;; This program is distributed in the hope that it will be useful,
|
|
|
|
|
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;;;; GNU General Public License for more details.
|
|
|
|
|
;;;;
|
|
|
|
|
;;;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
;;;; FLORA-SEARCH-AURORA.OVERWORLD
|
|
|
|
|
;;;; All game-functions and data relating to the “overworld” (that is,
|
|
|
|
|
;;;; the primary gameplay, the RPG-ish-ish bits).
|
|
|
|
|
|
|
|
|
|
(defpackage :flora-search-aurora.overworld
|
|
|
|
|
(:use :cl
|
|
|
|
|
:flora-search-aurora.input :flora-search-aurora.display
|
2023-06-16 09:36:53 -05:00
|
|
|
|
:flora-search-aurora.overworld.tiled
|
2023-06-09 07:07:28 -05:00
|
|
|
|
:flora-search-aurora.ui)
|
2023-06-16 09:36:53 -05:00
|
|
|
|
(:export #:overworld-state :player))
|
2023-06-09 07:07:28 -05:00
|
|
|
|
|
|
|
|
|
(in-package :flora-search-aurora.overworld)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
;;; Overworld loop
|
|
|
|
|
;;; ———————————————————————————————————
|
2023-06-10 15:29:47 -05:00
|
|
|
|
(defun overworld-state
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(matrix &key (map-path nil) (map (load-map map-path)))
|
2023-06-09 16:14:46 -05:00
|
|
|
|
"Render the given map to the matrix and take user-input — for one frame.
|
2023-06-11 17:12:52 -05:00
|
|
|
|
A state-function for use with STATE-LOOP."
|
2023-06-09 20:14:31 -05:00
|
|
|
|
(sleep .02)
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(overworld-state-draw matrix map)
|
|
|
|
|
(overworld-state-update map))
|
2023-06-09 07:07:28 -05:00
|
|
|
|
|
|
|
|
|
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(defun overworld-state-draw (matrix map)
|
2023-06-09 16:14:46 -05:00
|
|
|
|
"Draw the overworld map to the given matrix.
|
2023-06-11 17:12:52 -05:00
|
|
|
|
A core part of OVERWORLD-STATE."
|
2023-06-16 12:41:19 -05:00
|
|
|
|
(let* ((player-data (cdr (assoc 'player (gethash :entities map))))
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(chunk (world-coords-chunk (getf player-data :coords))))
|
2023-06-16 09:00:58 -05:00
|
|
|
|
(matrix-write-map-chunk matrix map chunk)
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(matrix-write-entities matrix map)))
|
2023-06-09 07:07:28 -05:00
|
|
|
|
|
|
|
|
|
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(defun overworld-state-update (map)
|
2023-06-11 17:12:52 -05:00
|
|
|
|
"Do nothing, lol. Core part of OVERWORLD-STATE.
|
|
|
|
|
Returns parameters to be used in the next invocation of OVERWORLD-STATE."
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(process-overworld-input map)
|
|
|
|
|
(list :map map))
|
2023-06-09 23:00:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
;;; Overworld logic
|
|
|
|
|
;;; ———————————————————————————————————
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(defun process-overworld-input (map)
|
2023-06-09 23:00:26 -05:00
|
|
|
|
"Get and process any keyboard input, modifying the map or entities as necessary."
|
|
|
|
|
(if (listen)
|
|
|
|
|
(let* ((input (normalize-char-plist (read-char-plist))))
|
|
|
|
|
(cond
|
2023-06-16 12:16:44 -05:00
|
|
|
|
((plist= input '(:modifier nil :char #\return))
|
2023-06-16 12:41:19 -05:00
|
|
|
|
(let ((interact (getf (cdr (assoc 'player (gethash :entities map)))
|
|
|
|
|
:interact)))
|
2023-06-16 12:16:44 -05:00
|
|
|
|
(if interact
|
|
|
|
|
(apply (intern interact) (list map)))))
|
2023-06-09 23:00:26 -05:00
|
|
|
|
((plist= input '(:modifier nil :char #\→))
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(move-entity map 'player :x 1))
|
2023-06-09 23:00:26 -05:00
|
|
|
|
((plist= input '(:modifier nil :char #\←))
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(move-entity map 'player :x -1))
|
2023-06-09 23:00:26 -05:00
|
|
|
|
((plist= input '(:modifier nil :char #\↑))
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(move-entity map 'player :y -1))
|
2023-06-09 23:00:26 -05:00
|
|
|
|
((plist= input '(:modifier nil :char #\↓))
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(move-entity map 'player :y 1))))))
|
2023-06-09 23:00:26 -05:00
|
|
|
|
|
|
|
|
|
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(defun move-entity (map entity &key (x 0) (y 0))
|
2023-06-09 23:00:26 -05:00
|
|
|
|
"Move an entity relative to its current position."
|
2023-06-16 12:41:19 -05:00
|
|
|
|
(let ((entity-plist (cdr (assoc entity (gethash :entities map)))))
|
2023-06-10 15:29:47 -05:00
|
|
|
|
(when (< x 0)
|
|
|
|
|
(setf (getf entity-plist :direction) 'left))
|
|
|
|
|
(when (> x 0)
|
|
|
|
|
(setf (getf entity-plist :direction) 'right))
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(move-entity-to map entity
|
2023-06-13 18:10:15 -05:00
|
|
|
|
:x (+ x (getf (getf entity-plist :coords) :x))
|
|
|
|
|
:y (+ y (getf (getf entity-plist :coords) :y)))))
|
2023-06-09 23:00:26 -05:00
|
|
|
|
|
|
|
|
|
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(defun move-entity-to (map entity &key (x 0) (y 0))
|
2023-06-09 23:00:26 -05:00
|
|
|
|
"Move the given entity to the given coordinates."
|
2023-06-16 12:41:19 -05:00
|
|
|
|
(let ((entity-plist (cdr (assoc entity (gethash :entities map)))))
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(when (walkable-tile-p map x y)
|
|
|
|
|
(setf (getf (getf entity-plist :coords) :x) x)
|
|
|
|
|
(setf (getf (getf entity-plist :coords) :y) y))))
|
|
|
|
|
|
|
|
|
|
|
2023-06-09 07:07:28 -05:00
|
|
|
|
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
;;; Mapping & map-rendering
|
|
|
|
|
;;; ———————————————————————————————————
|
2023-06-16 09:00:58 -05:00
|
|
|
|
(defun matrix-write-map-chunk (matrix map chunk
|
|
|
|
|
&key (chunk-width 72) (chunk-height 20))
|
2023-06-15 11:15:46 -05:00
|
|
|
|
"Draw a map’s specific chunk (by its ID) to the matrix."
|
2023-06-16 12:41:19 -05:00
|
|
|
|
(mapcar (lambda (cell)
|
2023-06-15 18:14:39 -05:00
|
|
|
|
(matrix-write-cell matrix cell))
|
2023-06-16 12:41:19 -05:00
|
|
|
|
(cdr (assoc chunk (gethash :tiles map)))))
|
2023-06-11 17:12:52 -05:00
|
|
|
|
|
|
|
|
|
|
2023-06-15 18:14:39 -05:00
|
|
|
|
(defun matrix-write-cell (matrix cell)
|
|
|
|
|
"Set a matrice's (2d array's) element corresponding to a “cell”; that is, an
|
|
|
|
|
alist containing a character (:CHAR) and :X & :Y coordinates."
|
|
|
|
|
(let ((coords (world-coords->screen-coords (getf cell :coords))))
|
2023-06-13 18:10:15 -05:00
|
|
|
|
(setf (aref matrix
|
|
|
|
|
(getf coords :y)
|
|
|
|
|
(getf coords :x))
|
2023-06-15 18:14:39 -05:00
|
|
|
|
(getf cell :char))))
|
2023-06-09 07:07:28 -05:00
|
|
|
|
|
|
|
|
|
|
2023-06-15 18:14:39 -05:00
|
|
|
|
(defun cell-at-world-coords-p (map-chunks coords)
|
|
|
|
|
"Return whether or not there is a cell at the given coordinates."
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(let ((chunk (world-coords-chunk coords)))
|
|
|
|
|
(member 't (cdr (assoc chunk map-chunks))
|
|
|
|
|
:test (lambda (ignored cell)
|
2023-06-15 18:14:39 -05:00
|
|
|
|
(plist= (getf cell :coords) coords)))))
|
2023-06-15 11:15:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun walkable-tile-p (map x y)
|
|
|
|
|
"Return whether or not the given coordinates on the map are traversable for an entity."
|
2023-06-16 12:41:19 -05:00
|
|
|
|
(not (cell-at-world-coords-p (gethash :bump-map map)
|
2023-06-15 18:14:39 -05:00
|
|
|
|
(list :x x :y y))))
|
2023-06-15 11:15:46 -05:00
|
|
|
|
|
|
|
|
|
|
2023-06-09 23:00:26 -05:00
|
|
|
|
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
;;; Entity magic (AKA player, NPCs)
|
|
|
|
|
;;; ———————————————————————————————————
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(defun matrix-write-entities (matrix map)
|
2023-06-09 23:00:26 -05:00
|
|
|
|
"Draw all entities from an alist of entities to the matrix."
|
|
|
|
|
(mapcar (lambda (entity-assoc)
|
2023-06-15 11:15:46 -05:00
|
|
|
|
(matrix-write-entity matrix (cdr entity-assoc)))
|
2023-06-16 12:41:19 -05:00
|
|
|
|
(gethash :entities map)))
|
2023-06-09 23:00:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun matrix-write-entity (matrix entity-plist)
|
|
|
|
|
"Render an entity-plist to the matrix."
|
2023-06-14 16:36:34 -05:00
|
|
|
|
(matrix-write-entity-head matrix entity-plist)
|
|
|
|
|
(matrix-write-entity-legs matrix entity-plist))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun matrix-write-entity-head (matrix entity-plist)
|
|
|
|
|
"Draw an entity’s head. There aren't any Mami Tomoes in this game, dang it!"
|
2023-06-13 18:10:15 -05:00
|
|
|
|
(let* ((screen-coords (world-coords->screen-coords (getf entity-plist :coords)))
|
2023-06-14 16:36:34 -05:00
|
|
|
|
(direction (getf entity-plist :direction))
|
|
|
|
|
(face (getf entity-plist :face))
|
|
|
|
|
(width (+ (length face) 2)) ;; Face + |borders|
|
|
|
|
|
(y (- (getf screen-coords :y) 1))
|
|
|
|
|
(x (if (eq direction 'right)
|
|
|
|
|
(- (getf screen-coords :x) (floor (/ width 2)) 0)
|
|
|
|
|
(- (getf screen-coords :x) (floor (/ width 2)) 0))))
|
2023-06-10 15:29:47 -05:00
|
|
|
|
(render-line matrix face (+ x 1) y)
|
2023-06-14 16:36:34 -05:00
|
|
|
|
(ignore-errors (setf (aref matrix y x) #\|))
|
|
|
|
|
(ignore-errors (setf (aref matrix y (+ width x -1))
|
|
|
|
|
#\|))))
|
2023-06-10 15:29:47 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun matrix-write-entity-legs (matrix entity-plist)
|
2023-06-14 16:36:34 -05:00
|
|
|
|
"Draw a bipdel entity’s legs — a surprisingly in-depth task!"
|
2023-06-13 18:10:15 -05:00
|
|
|
|
(let* ((screen-coords (world-coords->screen-coords (getf entity-plist :coords)))
|
|
|
|
|
(x (getf screen-coords :x))
|
2023-06-14 16:36:34 -05:00
|
|
|
|
(y (getf screen-coords :y))
|
2023-06-13 18:10:15 -05:00
|
|
|
|
(direction (getf entity-plist :direction)))
|
2023-06-10 15:29:47 -05:00
|
|
|
|
(cond ((eq direction 'right)
|
2023-06-14 16:36:34 -05:00
|
|
|
|
(ignore-errors (setf (aref matrix y x) #\|))
|
|
|
|
|
(ignore-errors (setf (aref matrix y (- x 1)) #\|)))
|
2023-06-16 12:16:44 -05:00
|
|
|
|
('t
|
2023-06-14 16:36:34 -05:00
|
|
|
|
(ignore-errors (setf (aref matrix y x) #\|))
|
|
|
|
|
(ignore-errors (setf (aref matrix y (+ x 1)) #\|))))))
|
2023-06-09 23:00:26 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
;;; Misc. utility
|
|
|
|
|
;;; ———————————————————————————————————
|
|
|
|
|
(defun every-other-element (list)
|
|
|
|
|
"Collect every-other-element of a list. E.g., (1 2 3 4) → (1 3)."
|
|
|
|
|
(when list
|
|
|
|
|
(cons (car list)
|
|
|
|
|
(every-other-element (cddr list)))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun plist= (a b &key (test #'eql))
|
|
|
|
|
"Return whether or not two property lists are equal, by comparing values of each pair.
|
|
|
|
|
Uses the keys of plist a."
|
|
|
|
|
(let ((keys (every-other-element a)))
|
|
|
|
|
(loop for key in keys
|
|
|
|
|
do (when (not (apply test (list (getf a key)
|
|
|
|
|
(getf b key))))
|
|
|
|
|
(return nil))
|
|
|
|
|
finally (return 't))))
|