; Cell Stuff
; This will result in a "2x2" cell, of [0 1 1 1], with 0 representing an empty
; space, and 1 representing a wall.
(var (hall-width wall-width) (values 2 2))

; This sets the "map width". This means 10 cells across and down. With each cell
; being 2x2, this results in a 20x20 square map. All maps will be square (for
; now).
(var cell-num 10)

; Explicitly setting cell-size for convenience. All cells are squares
(var cell-size (+ hall-width wall-width))

; Initialize the cell table
(var cells {})
(for [c 1 (* cell-num cell-num)]
	(var cell [])
	(for [i 1 cell-size]
			(for [j 1 cell-size]
					(var cell-index (+ j (* (- i 1) cell-size)))
					(if (and (< i (+ hall-width 1)) (< j (+ hall-width 1)))
							(tset cell cell-index 0)
							(tset cell cell-index 1))))
	(table.insert cells cell))

(fn cell-row [i] (+ 1 (// (- i 1) cell-num)))
(fn map-index [i] 
	(var cell-col (if (= 0 (% i cell-num)) cell-num (% i cell-num)))
	(+ (* cell-size cell-size cell-num (- (cell-row i) 1))
		 (- (* cell-col cell-size) (- cell-size 1))))
(fn generate_cell_map [cells]
	(var cell_map [])
	(for [c 1 (length cells)]
		(var (mi ci) (values (map-index c) 1))
		(for [i 1 cell-size]
			(for [j 1 cell-size]
				(var new-map-index (+ mi (+ (- i 1) (* (- j 1) (* cell-num cell-size)))))
				(var new-cell-index (+ ci (+ (- i 1) (* (- j 1) cell-size)))) 
				(tset cell_map new-map-index (. cells c new-cell-index)))))
	cell_map)

(fn print_cell_map [cells]
	(var output "")
	(for [i 1 (length cells)]
		(set output (.. output " " (. cells i)))
		(if (= 0 (% i (* cell-num cell-size))) (set output (.. output "\n"))))
	(print output))

(print_cell_map (generate_cell_map cells))

; (var output "")
; (var ctp (. cells 51))
; (for [i 1 (length ctp)]
; 	(set output (.. output " " (. ctp i))))
; (print output)
