                                               面面                        面面                                                      面                        面                      面                            面                         面                      面面                           面                        面面                          鰐                                                       面                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             0000000000000000@@@@@@@@@@@@@@@@PPPPPPPPPPPPPPPP````````````````pppppppppppppppp																















旭旭旭旭旭旭旭旭誓誓誓誓誓誓誓誓佩佩佩佩佩佩佩佩玻玻玻玻玻玻玻玻韶韶韶韶韶韶韶韶	>                                                              =                                                                                                                                                                                                                                                                                                                                                                           面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             面面面面面面面   面面      鰐面   面面   面面面面          鰐面面面面面面面面面   面面面面   面面                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   面面               面面   鰐面   面面面面面面面面面面面面   面面             面                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 鰐面                  面面          鰐面面面面面      面面面面   面面面面面面   面面面面面面   鰐面面                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       面                           面面面面   面面面面面面面面   面面         面面   面面   面面面面                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         面   面面面面面面面面面面   面面           面面面面面面   面面面面   面面      鰐面       面面      面                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      面面   鰐面面面   面面   面面   鰐面   面面   鰐面      鰐面         面                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       面面面面面         面面      鰐面面面      面面面面   面面面面面面      面面                鰐                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             面      鰐面面面面面            面面面面   鰐面面面      面面面面面面面面         面面面面面面面面面                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  面面面面面      鰐面   面面面面         面面   面面      面面      鰐面面面面面               鰐                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           鰐面面面                面面             面面      面面面面面面   鰐面      面面       面  面面                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           鰐面面面   面面         面面           面面   面面面面面面   面面面面           面面面面   面面                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   面面                     面面      鰐面      面面      面面   面面      鰐面面面面面面面 面面                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  面面面   鰐面   鰐面面面面面   面面面面面面   面面      面面   面面   面面   面面   面面面面   面                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 面面   面面面面   面面   面面         面面       面面面面         面面面面面面面面面面面面 面面                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    面面面面面面面面面面       面面面面   面面面面面面面面面面面面   面面面面   面面   面面      鰐面面面面                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面面u ; title:  Wave Function Collapse
; author: www.crisjr.eng.br
; desc:   Studying this wave function collapse thingy
; script: fennel

; ########
; # DATA #
; ########
; for all intents and purposes, directions will be
; described as such:
;     1
;     |
; 2 --+-- 0
;     |
;     3
(global H0 "abefhikn") ; these allow neighbours
(global H1 "acdehjkm")
(global H2 "abdgijkl")
(global H3 "acfghijo")
(global E0 "zcdgjlmo") ; these don't
(global E1 "zbfgilno")
(global E2 "zcefhmno")
(global E3 "zbdeklmn")
(global POSSIBLE-STATES "abcdefghijklmnoz")
(global STATE-SPRITES
	{"z" 0
	 "a" 1
		"b" 2
		"c" 3
		"d" 4
		"e" 5
		"f" 6
		"g" 7
		"h" 8
		"i" 9
		"j" 10
		"k" 11
		"l" 12
		"m" 13
		"n" 14
		"o" 15})
(global POSSIBLE-NEIGHBOURS
	{"z" [E0 E1 E2 E3]
	 "a" [H0 H1 H2 H3]
		"b" [H0 E1 H2 E3]
		"c" [E0 H1 E2 H3]
		"d" [H0 E1 E2 H3]
		"e" [E0 E1 H2 H3]
		"f" [E0 H1 H2 E3]
		"g" [H0 H1 E2 E3]
		"h" [E0 H1 H2 H3]
		"i" [H0 H1 H2 E3]
		"j" [H0 H1 E2 H3]
		"k" [H0 E1 H2 H3]
		"l" [H0 E1 E2 E3]
		"m" [E0 E1 E2 H3]
		"n" [E0 E1 H2 E3]
		"o" [E0 H1 E2 E3]})
(global WIDTH 30)
(global HEIGHT 17)
(global UNDETERMINED-STATE-SPRITE 255)
(global UNDETERMINED-BASE-STATE 239)
; data to be manipulated
(var LOOP nil)
(var BOARD nil)
(var TODO-LIST nil)

; ##########################
; # FUNCTIONAL PROGRAMMING #
; ##########################

(fn dec [x]
	(- x 1))

(fn inc [x]
	(+ 1 x))
	
(fn f2i [f]
	(math.floor f))
	
(fn nths [s n]
	(string.sub s n n))
	
(fn fsfilter [f xs]
	(var ys "")
	(for [i 1 (length xs) 1]
		(let [xi (nths xs i)]
			(when (f xi)
				(set ys (.. ys xi)))))
	ys)
	
(fn random-int [upper]
	(-> upper
		   math.random
					f2i))
					
; #########
; # MODEL #
; #########

; # NEW BOARD
(fn new-board--insert-top-row [state]
	(do
		(var row ["d"])
		(for [i 0 (- WIDTH 3) 1]
			(table.insert row "b"))
		(table.insert row "e")
		(table.insert state row)
		state))
	
(fn new-middle-row []
	(do
		(var row ["c"])
		(for [i 0 (- WIDTH 3) 1]
			(table.insert row POSSIBLE-STATES))
		(table.insert row "c")
		row))
(fn new-board--insert-middle-rows [state how-many-times]
	(do
  (for [i 0 how-many-times 1]
				(table.insert state (new-middle-row [])))
		state))
	
(fn new-board--insert-bottom-row [state]
	(do
		(var row ["g"])
		(for [i 0 (- WIDTH 3) 1]
			(table.insert row "b"))
		(table.insert row "f")
		(table.insert state row)
		state))

; TODO maybe filter states based on borders too
(fn new-board []
	(-> []
	    (new-board--insert-top-row)
					(new-board--insert-middle-rows (- HEIGHT 3))
					(new-board--insert-bottom-row)))
					
; #####################
; # PROCESSING ENGINE # 
; #####################

(fn add-task--set [where what]
	(table.insert TODO-LIST {"how" "set"
     	                    "where" where
                       			"what" what}))

(fn add-task--filter [where what]
	(table.insert TODO-LIST {"how" "filter"
             	            "where" where
             													"what" what}))

; TODO add task from an actual ambigous state instead
;      of picking a random coordinate and testing it
(fn maybe-add-task--set-random []
	(let [x (random-int WIDTH)
	      y (random-int HEIGHT)
	      b (. BOARD y x)]
		(when (> (length b) 1)
			(add-task--set [x y]
              				(nths b (random-int (length b)))))))

(fn new-todo-list []
 (do
 	(set TODO-LIST [])
	 ; exploiting the fact I know the board's initial state
 	(add-task--set [(f2i (/ WIDTH 2)) 
	                 (f2i (/ HEIGHT 2))]
	                "a")
		TODO-LIST))
                          
(fn run-task--check-substring [f s]
	(~= nil (string.find f s 1 true)))

; TODO add more filter tasks based on current one	
(fn run-task--filter [task]
	(let [[i j] (. task "where")
	      v (. task "what" "value")
							d (. task "what" "direction")
							f (. POSSIBLE-NEIGHBOURS v (inc d))
							b (. BOARD j i)]
		(when (> (length b) 1)
   (tset BOARD j i 
         (fsfilter #(run-task--check-substring f $1) 
 	                 b)))))
			
(fn run-task--set [task]
	(let [[i j] (. task "where")
	      v (. task "what")]
		(do
			(tset BOARD j i v)
			(when (< (inc i) (dec WIDTH))
				(add-task--filter [(inc i) j] 
				                  {"value" v
																						 "direction" 0}))
			(when (> (dec j) 0)
				(add-task--filter [i (dec j)] 
				                  {"value" v
																						 "direction" 1}))
			(when (> (dec i) 0)
				(add-task--filter [(dec i) j] 
				                  {"value" v
																						 "direction" 2}))
			(when (< (inc j) (dec HEIGHT))
				(add-task--filter [i (inc j)] 
				                  {"value" v
																						 "direction" 3})))))

(fn task-engine []
 (do
	 (let [[task & tasks] TODO-LIST]
			(when task
				(let [how (. task "how")]
					(if (= "set" how) (run-task--set task)
					    (= "filter" how) (run-task--filter task)
									(trace (.. "unknown task " how)))))
			(if (> (length tasks) 0)
				(set TODO-LIST tasks)
				(maybe-add-task--set-random)))))

; ########
; # VIEW #
; ########
(fn draw-board []
 (for [j 0 (dec HEIGHT) 1] (for [i 0 (dec WIDTH) 1]
		(let [possible-states (. BOARD (inc j) (inc i))
		      s (if (= 1 (length possible-states))
								   (. STATE-SPRITES possible-states)
											(+ (length possible-states)
											   UNDETERMINED-BASE-STATE))
								x (* i 8)
								y (* j 8)]
			(spr s x y)))))

; ##############
; # CONTROLLER #
; ##############

; # WAVE FUNCTION COLLAPSE
(fn collapse-loop []
	(do
		; update
		(task-engine)
		; draw
		(cls)
		(draw-board)))

(fn collapse-setup []
	(do
		(set BOARD (new-board))
		(set TODO-LIST (new-todo-list))
		(set LOOP collapse-loop)))

; ##################
; # MAIN FUNCTIONS #
; ##################

(fn SETUP []
	(collapse-setup))

(SETUP)
(global TIC
	(fn tic []
		(LOOP)))