diff --git a/util.lisp b/util.lisp index a363df0..c645166 100644 --- a/util.lisp +++ b/util.lisp @@ -202,7 +202,11 @@ "Return n plus a random offset" (+ n (- (random (* 2 max-offset)) max-offset))) -(defun chancep (percent) +(defun chancep (p) + "Do a random test with 1/p probability of success." + (= 1 (random p))) + +(defun probabilityp (percent) "Do a random test, with the percentage giving the success probability" ;;Accuracy: 0.1 (> percent (/ (random 1000) 10))) diff --git a/util.lisp b/util.lisp index a363df0..c645166 100644 --- a/util.lisp +++ b/util.lisp @@ -202,7 +202,11 @@ "Return n plus a random offset" (+ n (- (random (* 2 max-offset)) max-offset))) -(defun chancep (percent) +(defun chancep (p) + "Do a random test with 1/p probability of success." + (= 1 (random p))) + +(defun probabilityp (percent) "Do a random test, with the percentage giving the success probability" ;;Accuracy: 0.1 (> percent (/ (random 1000) 10))) diff --git a/world.lisp b/world.lisp index 40bcffb..5f79f46 100644 --- a/world.lisp +++ b/world.lisp @@ -6,7 +6,7 @@ ;;;; (c) 2018 Daniel Vedder, MIT license ;;;; -(defvar *world* NIL) +(defparameter *world* NIL) (defconstant *directions* '(N NE E SE S SW W NW)) (defstruct patch @@ -100,17 +100,21 @@ ;; TOPOGRAPHY FUNCTIONS +(defun print-topography (&optional (stream T) (world *world*)) + "Print a text representation of the world and each patch's biome" + (dolist (row world) + (format stream "~&~A~%" + (string-from-list + (mapcar #'(lambda (p) (biome-char (patch-biome p))) row) + "")))) + (defun save-topography (file-name &optional (world *world*)) "Save the world topography as a text file" (debugging "~&Saving world to file ~A" file-name) ;debug (with-open-file (tf file-name :direction :output) - (dolist (row world) - (format (if file-name tf T) "~&~A~%" - (string-from-list - (mapcar #'(lambda (p) (biome-char (patch-biome p))) row) - " "))))) - -(defun generate-biomes (size-factor &optional (world *world*)) + (print-topography tf))) + +(defun generate-biomes (size-factor world) ;;XXX The maps this produces don't look as expected, but for ;; current purposes they are good enough (debugging "~&Generating biomes") ;debug @@ -134,18 +138,18 @@ (setf (patch-biome (coord x y world)) (get-biome b))))))) -(defun generate-stream (x0 y0 &optional (world *world*)) +(defun generate-stream (x0 y0 world) (debugging "~&Generating a stream, starting at ~S/~S" x0 y0) ;debug (do* ((dir (random-elt *directions*) - (if (chancep 60) dir (next-dir dir (random-elt '(T NIL))))) + (if (probabilityp 60) dir (next-dir dir (random-elt '(T NIL))))) (patch (coord x0 y0 world) (neighbour patch dir world))) ((or (null patch) (eq (patch-biome patch) (get-biome 'stream)))) (setf (patch-biome patch) (get-biome 'stream)))) -(defun create-world (size &optional (world *world*)) - (setf world NIL) - (setf world (init-matrix size)) - ;;XXX magic numbers - (generate-biomes 20 world) - (dotimes (s (round (/ (expt size 2) 500))) - (generate-stream (random size) (random size) world))) +(defun create-world (size) + (let ((world (init-matrix size))) + ;;XXX magic numbers + (generate-biomes 20 world) + (dotimes (s (round (/ (expt size 2) 500))) + (generate-stream (random size) (random size) world)) + world))