diff --git a/terranostra.lisp b/terranostra.lisp index a194b61..cfc8ab3 100644 --- a/terranostra.lisp +++ b/terranostra.lisp @@ -1,4 +1,4 @@ -#!/usr/bin/clisp +;#!/usr/bin/clisp ;;;; ;;;; Terra Nostra is a Minecraft-like survival game for the commandline. ;;;; @@ -8,6 +8,8 @@ ;;(load "util.lisp") ;; I want to avoid using this if possible (defvar *world* NIL) +(defconstant *directions* '(N NE E SE S SW W NW)) + (defstruct patch (pos '(0 0)) ;position @@ -15,6 +17,7 @@ (streams-in NIL) (streams-out NIL)) + (defun init-matrix (size) "Create a square matrix of empty patches" (do ((y 0 (1+ y)) (world NIL) (row NIL NIL)) @@ -28,6 +31,11 @@ (unless (or (< x 0) (< y 0) (> x (length world)) (> y (length world))) (nth x (nth y world)))) +(defun opposite-dir (dir) + "Return the direction opposite the input" + (let ((pos (position dir *directions*))) + (when pos (nth (rem (+ 4 pos) 8) *directions*)))) + (defun dir2patch (herex herey therex therey) "Calculate the direction to a patch" (cond ((> herex therex) @@ -42,18 +50,28 @@ ((< herey therey) 'N) (T NIL))))) +(defun coordsindir (x y dir) + "Return the coordinates in the given direction" + (cond ((eq dir 'N) (list x (1- y))) + ((eq dir 'NE) (list (1+ x) (1- y))) + ((eq dir 'E) (list (1+ x) y)) + ((eq dir 'SE) (list (1+ x) (1+ y))) + ((eq dir 'S) (list x (1+ y))) + ((eq dir 'SW) (list (1- x) (1+ y))) + ((eq dir 'W) (list (1- x) y)) + ((eq dir 'NW) (list (1- x) (1- y))) + ((null dir) (list x y)) + (T (error "~&Invalid direction ~S")))) + (defun patchindir (x y dir &optional (world *world*)) "Return the patch in the given direction" - (cond ((eq dir 'N) (coord x (1- y) world)) - ((eq dir 'NE) (coord (1+ x) (1- y) world)) - ((eq dir 'E) (coord (1+ x) y world)) - ((eq dir 'SE) (coord (1+ x) (1+ y) world)) - ((eq dir 'S) (coord x (1+ y) world)) - ((eq dir 'SW) (coord (1- x) (1+ y) world)) - ((eq dir 'W) (coord (1- x) y world)) - ((eq dir 'NW) (coord (1- x) (1- y) world)) - ((null dir) (coord x y world)) - (T (error "~&Invalid direction ~S")))) + (let* ((coords (coordsindir x y dir)) + (nextx (first coords)) (nexty (second coords))) + (coord nextx nexty world))) + +(defun neighbour (p dir &optional (world *world*)) + "Return the neighbouring patch in this direction" + (patchindir (first (patch-pos p)) (second (patch-pos p)) dir world)) (defun save-topography (file-name &optional (world *world*)) "Save the world topography as a csv file" @@ -93,9 +111,7 @@ (defun create-stream (x y world) "Create a stream, starting at x/y" ;;TODO - (flet ((lowest-neighbour ()))) ) - ;; Initialize the random state (which would otherwise not be very random...) (setf *random-state* (make-random-state t))