
;;;;;;;;;;;;;;;;;;;;;;;;;;
; Deeps template maps

(kern-mk-map 'm_deeptempl_wall 19 19 pal_expanded
		(list
			"rr rr rr rr rr rr rr rr rr rr rr rr rr rr rr rr rr rr rr "
			"rr rr rr rr rr rr rr rr rr rr rr rr rr rr rr rr rr rr rr "
			"rr rr rr rr rr .. .. rr {e rr rr {4 {{ rr rr rr rr rr rr "
			"rr rr rr rr rr .. .. .. .. bb .. .. .. .. rr rr rr rr rr "
			"rr rr rr {{ {2 .. .. {8 .. .. .. .. {8 .. {4 {{ rr rr rr "
			"rr rr .. {1 {8 .. rr {{ {2 {8 .. {4 {{ rr .. {1 .. rr rr "
			"rr rr rr {4 {{ {6 {{ rr rr {{ {a .. {1 .. .. .. .. rr rr "
			"rr rr .. .. {1 .. {5 {{ rr rr {{ rr .. .. {8 .. {4 {{ rr "
			"rr .. bb .. .. .. .. {1 rr rr rr {8 .. bb {{ {2 .. rr rr "
			"rr .. .. .. .. rr .. {8 .. rr {{ {{ {2 .. {1 .. rr rr rr "
			"rr rr .. .. {4 {{ rr {{ rr rr rr rr .. .. .. .. bb rr rr "
			"rr .. .. .. .. {1 .. {9 rr {{ {6 {{ rr .. .. .. .. .. rr "
			"rr rr {8 .. bb .. {4 {{ rr {1 .. {1 .. .. .. .. .. .. rr "
			"rr rr {{ .. .. .. .. rr .. {8 .. .. .. .. bb {8 .. rr rr "
			"rr rr rr .. rr .. .. .. {4 {{ bb .. .. .. {4 {{ rr rr rr "
			"rr rr rr rr {{ {2 .. .. .. {1 .. .. .. .. .. rr rr rr rr "
			"rr rr rr rr rr {{ {2 .. {c {{ {{ {2 bb rr rr rr rr rr rr "
			"rr rr rr rr rr rr .. rr {{ rr rr .. rr rr rr rr rr rr rr "
			"rr rr rr rr rr rr rr rr rr rr rr rr rr rr rr rr rr rr rr "
		)
	)
 
(kern-mk-map
  'm_deeptempl_passage 19 19 pal_expanded
		(list
			"rr rr rr rr rr rr .. .. .. .. {4 {{ {2 .. rr rr rr rr rr "
			"rr rr rr rr rr rr .. .. .. .. .. {1 .. rr rr rr rr rr rr "
			"rr rr rr rr rr .. .. .. .. .. .. .. .. .. rr rr rr rr rr "
			"rr rr rr {{ {2 .. .. .. .. .. .. .. .. .. {4 {{ rr rr rr "
			"rr rr rr {1 .. .. {8 .. .. .. .. .. .. .. .. rr rr rr rr "
			".. rr .. .. .. {4 {{ {2 .. .. .. .. .. .. {8 .. .. {8 rr "
			".. {8 .. .. .. .. {1 .. .. {8 .. .. .. {4 {{ {2 {4 {{ {2 "
			"{4 {{ {2 .. .. .. .. .. {4 {{ {2 .. .. .. {1 .. .. {1 .. "
			".. {1 .. .. .. .. .. bb .. {1 .. .. .. .. .. .. .. .. .. "
			".. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. "
			".. .. .. .. .. .. .. .. .. .. .. {8 .. .. .. .. .. .. .. "
			".. .. bb .. .. .. .. .. .. .. {4 {{ {2 .. .. .. .. .. .. "
			".. .. .. .. .. .. .. {8 .. .. .. {1 .. .. .. .. .. .. .. "
			"rr .. .. .. .. .. {4 {{ {2 .. .. .. .. .. .. .. .. rr .. "
			"rr rr rr .. {8 .. .. {1 .. .. .. .. .. .. .. .. rr rr rr "
			"rr rr rr rr {{ {2 .. .. .. .. .. .. .. .. .. bb rr rr rr "
			"rr rr rr rr rr .. .. .. .. {8 .. .. .. rr rr rr rr rr rr "
			"rr rr rr rr rr rr .. .. {4 {{ {a .. .. rr rr rr rr rr rr "
			"rr rr rr rr rr .. .. .. .. {5 {{ {2 .. .. rr rr rr rr rr "
		)
	)
	
 
(kern-mk-map
  'm_deeptempl_water 19 19 pal_expanded
		(list
			"rr rr rr rr rr rr ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ rr rr rr rr rr "
			"rr rr rr rr rr ~b ~8 ~8 ~~ ~~ ~~ ~8 ~~ ~4 rr rr rr rr rr "
			"rr rr rr rr rr .. .. .. ~% ~e %c .. ~% ~e rr rr rr rr rr "
			"rr rr rr rr rr .. .. .. .. .. .. .. .. .. rr rr rr rr rr "
			"rr rr rr rr .. .. .. .. .. .. .. .. .. .. .. .. rr rr rr "
			"~~ ~5 .. .. .. .. ~C ~7 ~A .. .. bb .. .. .. .. .. ~b ~~ "
			"~~ ~c .. .. .. .. ~3 -- ~d .. .. .. .. .. .. .. .. bb ~~ "
			"~~ %c bb .. .. .. ~a ~~ {G {2 .. .. .. .. .. .. .. ~3 ~~ "
			"~4 .. .. .. .. .. %a ~e {1 .. .. .. .. .. .. .. .. ~2 ~~ "
			"~~ ~A .. .. .. .. .. .. .. .. .. ~7 .. .. .. .. .. ~a ~~ "
			"~~ ~d .. .. .. .. .. .. .. .. %3 ~~ ~5 ~A .. .. .. ~D ~~ "
			"~~ %c .. .. .. ~C ~7 ~A .. .. ~3 -- -- ~d .. .. .. ~3 ~~ "
			"~4 .. .. .. .. ~3 -- ~d .. ~b ~~ -- ~c ~# .. .. .. ~2 ~~ "
			"~c .. .. .. .. ~a ~c ~# .. .. %a ~e ~# .. .. .. .. ~e rr "
			"rr rr rr .. .. .. .. .. .. .. .. .. .. .. bb .. rr rr rr "
			"rr rr rr rr rr .. .. .. .. .. .. .. .. .. rr rr rr rr rr "
			"rr rr rr rr rr .. ~C ~7 ~A .. .. ~C ~3 ~d rr rr rr rr rr "
			"rr rr rr rr rr rr ~3 ~~ ~~ ~5 ~E ~3 ~~ rr rr rr rr rr rr "
			"rr rr rr rr rr ~3 ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~5 rr rr rr rr rr "
		)
	)
	
	
(kern-mk-map
	'm_deeptempl_hole 19 19 pal_expanded
		(list
			"rr rr rr rr rr rr vv vv vv vv vv vv vv *c rr rr rr rr rr "
			"rr rr rr rr rr *b vv vv *8 *c .i *e .g rr rr rr rr rr rr "
			"rr rr rr rr rr .h *e .g .. .. .. .. .. .. rr rr rr rr rr "
			"rr rr rr .. .. .. .. .. .. .. .. .. .. .. .. .. rr rr rr "
			"rr rr rr .. .. *7 {j .. .. .. .. .. {8 .. .. .. rr rr rr "
			"*5 rr .. .. .. *a vv *5 .j .. .. {4 {{ {a .. .. rr .. *3 "
			"vv *5 .. .. .. .. .h vv *5 .j .. .. bb {{ {2 .. .. .l vv "
			"vv *4 .. .. .. .. .. *a vv *5 .j .. .. {1 .. .. *b vv vv "
			"vv *c .. .. .. .. {4 {h vv vv *5 .j .. .. .. .. .h *a vv "
			"vv .g .. .. .. {8 .. .. *2 vv vv *d .. .. .. .. .. .m vv "
			"vv .j .. .. {c {{ {2 .. *a vv vv .g .. .. {8 .. .. *b vv "
			"vv *d .. {4 {{ rr .. .. rr *a vv {j {2 {4 {{ {2 .. .m vv "
			"vv .k .. .. {1 .. .. .. .. .h *a *5 .j .. {1 .. .. *3 vv "
			"vv *d .. .. .. .. .. .. .. .. .h *a *5 .. .. .. .. *e rr "
			"rr rr rr .. .. .. .. .. .. .. {4 {h *e .. .. .. rr rr rr "
			"rr rr rr .. .. .. .. .. .. .. .. {1 .. .. .. .. rr rr rr "
			"rr rr rr rr rr .. .. .. .. .. *7 .j .. .. rr rr rr rr rr "
			"rr rr rr rr rr *7 .n *3 *5 .n vv *5 .n *7 rr rr rr rr rr "
			"rr rr rr rr rr vv vv vv vv vv vv vv vv vv rr rr rr rr rr "
		)
	)

  (kern-mk-map 'm_deeptempl_lava 19 19 pal_expanded
		(list
			"rr rr rr rr rr rr !_ !_ !_ !_ !_ !_ !_ rr rr rr rr rr rr "
			"rr rr rr rr rr rr !! !! !! !! !! !! !_ !! rr rr rr rr rr "
			"rr rr rr rr rr !! !c .. .. .. bb !a !c .. rr rr rr rr rr "
			"rr rr rr bb rr .. .. .. .. .. .. .. .. .. {4 {{ rr rr rr "
			"rr rr rr .. .. .. .. .. .. .. .. .. .. .. .. {1 rr rr rr "
			"rr !! !! .. .. .. rr .. .. .. .. !3 !! !5 .. .. !! rr rr "
			"!_ !_ !c .. .. .. .. {8 .. .. .. !! !_ !c .. .. !! !_ !_ "
			"!_ !! {# .. .. .. {4 {{ {2 .. .. !a !c .. {8 .. !a !! !_ "
			"!_ !! {1 .. bb .. .. {1 .. .. .. .. .. {4 {{ {2 .. !! !_ "
			"!_ !! .. .. .. .. .. !7 .. .. .. .. .. .. {1 .. .. !! !_ "
			"!_ !! .. .. .. .. !3 !! !5 .. .. .. .. .. .. .. .. !! !_ "
			"!_ !! .. .. .. !3 !_ !! !c .. .. .. bb .. .. .. .. !! !_ "
			"!_ !! !5 .. .. !a !c .. .. .. .. !3 !5 .. .. .. {8 !! !_ "
			"!_ rr !! .. .. .. .. .. .. .. .. !a !c .. .. {4 {{ rr !_ "
			"rr rr rr .. {8 .. .. .. .. .. .. .. .. .. .. rr rr rr rr "
			"rr rr rr rr {{ {2 .. .. .. .. .. .. .. .. .. bb rr rr rr "
			"rr rr rr rr rr .. .. !7 bb .. .. !3 !! !! rr rr rr rr rr "
			"rr rr rr rr rr !! !! !! !! !! !! !! !_ rr rr rr rr rr rr "
			"rr rr rr rr rr rr !_ !_ !_ !_ !_ !_ !_ !_ rr rr rr rr rr "
		)
	)
	
  (kern-mk-map 'm_deeptempl_swamp 19 19 pal_expanded
		(list
			"bb rr {{ rr rr %a %% %% %% %% %% %% ~f %% rr rr rr rr rr "
			"rr rr rr rr rr {% %a %c {4 {% %% %% %% %c rr rr rr rr rr "
			"rr rr rr rr rr rr .. .. .. {1 %a %c {B {2 rr rr rr rr rr "
			"rr rr rr .. rr %7 .. .. bb .. %3 %% %% %% %d rr rr rr rr "
			"rr rr rr .. %b %% %5 .. %3 %% %% %c {8 .. .. rr rr rr rr "
			"rr {{ {2 .. .. %a %% %% %% %c .. {4 {{ {2 .. .. {{ %7 rr "
			"%% %5 .. .. bb .. .. %% %% .. .. .. {1 .. .. .. {1 %% %% "
			"%% ~f %5 .. .. .. %3 %% %% %5 .. {8 .. %7 .. .. %3 %% ~f "
			"%% %5 %% %a %5 .. %% ~b ~d %% %d {{ %3 %% %% %c %a %% %% "
			"%% %% .. .. %a %% %% %% %% %c rr rr %% %% %c .. .. .. %% "
			"%% %% bb .. .. %a %% %% %% .. %3 ~e %% .. .. .. .. {8 %% "
			"%% %c .. .. .. .. %% .. %a %% %% %% %c .. {8 .. {4 {C %% "
			"%% .. .. .. .. %3 %% .. .. %a %% %% .. {4 {{ {2 .. %b %% "
			"%c rr .. .. .. %% ~f %5 .. .. .. %% .. .. {1 .. .. rr %a "
			"rr rr rr .. .. %% %% %c .. .. .. %% %5 .. .. rr rr rr rr "
			"rr rr rr .. rr %a %c .. .. bb .. %a %% %% %d rr rr rr rr "
			"rr rr rr rr rr .. {8 .. .. .. %3 %% %5 {# rr rr rr rr rr "
			"rr rr rr rr rr rr {C %3 %% %% %% %% %% rr rr rr rr rr rr "
			"rr rr rr rr rr %3 %% %% %% %% %% ~f %% %5 rr rr rr rr rr "
		)
	)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Room data

(mk-obj-type 't_roomdata nil nil layer-none monman-ifc)

;utility for searching {sum-of-probability, object} lists
(define (get-numbered-elem list value)
	(if (>= (car (car list)) value)
		(cdar list)
		(get-numbered-elem (cdr list) value)
		)
	)

; returns roomdata object or nil if none
(define (get-roomdata kplace)
	(let ((dataslist (kplace-get-objects-of-type kplace t_roomdata)))
		(if (equal? (length dataslist) 0)
			nil
			(gob (car dataslist)))))
			

; replaces any roomdatas in place with given data
(define (set-roomdata kplace data)
	(begin
		; remove any/all previous data
		(map (lambda (rdataobj)
				(kern-obj-remove rdataobj))
			(kplace-get-objects-of-type kplace t_roomdata))
		;add new t_roomdata
		(kern-obj-put-at
			(bind (kern-obj-set-visible (kern-mk-obj t_roomdata 1) #f) data)
			(list kplace 0 0))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Deeps roomdata

(define (deeps-roomdata-x data)
	(list-ref data 0))
(define (deeps-roomdata-y data)
	(list-ref data 1))
(define (deeps-roomdata-current data)
	(list-ref data 2))
(define (deeps-roomdata-prev data)
	(list-ref data 3))
(define (deeps-roomdata-rooms data)
	(list-ref data 4))

(define (deeps-roomdata-setxy data x y)
	(set-car! data x)
	(set-car! (cdr data) y))

;prev becomes current, current is cleared
(define (deeps-roomdata-pushcurrent data)
	(let ((curdat (list-tail data 2)))
		(set-car! (cdr curdat) (car curdat))
		(set-car! curdat #f)))

(define (deeps-roomdata-setcurrent data cur)
	(let ((curdat (list-tail data 2)))
		(set-car! curdat cur)))

	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Deeps random map

; order is N W E S
(define (get-cardinal-ref list dir)
	(begin
	(list-ref list
		(/ (- dir 1) 2))
		))

;probability for edge terrains... out of 83
(define deep-terrain-edges
	(list
		(list 25 'm_deeptempl_wall)
		(list 30 'm_deeptempl_water)
		(list 35 'm_deeptempl_hole)
		(list 40 'm_deeptempl_lava)
		(list 45 'm_deeptempl_swamp)		
		(list 100 'm_deeptempl_passage)
	))

;probability for area terrains... out of 149
(define deep-terrain-area
	(list
		(list 30 'm_deeptempl_wall)
		(list 45 'm_deeptempl_water)
		(list 65 'm_deeptempl_hole)		
		(list 75 'm_deeptempl_lava)		
		(list 80 'm_deeptempl_swamp)		
		(list 150 'm_deeptempl_passage)
	))

(define deep-room-blitstats
	(list
		(list 5 0 9 3)
		(list 0 5 3 9)
		(list 16 5 3 9)
		(list 5 16 9 3)
		(list 3 3 13 13)
	)
)

(define deep-room-offsets
	(list 
		(list 0 1)
		(list -1 0)
		(list 1 0)
		(list 0 -1)
	)
)

(define deep-random-type-ew (list 53 67 43 83 'deep-terrain-edges))
(define deep-random-type-ns (list 59 71 31 83 'deep-terrain-edges))
(define deep-random-type-area (list 89 61 59 149 'deep-terrain-area))

(define (deep-get-template rxloc ryloc maptype)
	(let* (
			(xmult (list-ref maptype 0)) 
			(ymult (list-ref maptype 1)) 
			(addfactor (list-ref maptype 2)) 
			(modfactor (list-ref maptype 3)) 
			(maplist (eval(list-ref maptype 4)))
			(mapnumber (modulo (+ (* rxloc xmult) (* ryloc ymult) addfactor) modfactor))
		)
		;get the map from the first entry with value greater than mapnumber		
		(cadr
			(car (filter (lambda (listentry)
				(> (car listentry) mapnumber))
					maplist))
			)
	))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; deeps fixed map enabling

(define (deep-room-mklink dir target maptepl)
	(let* ((node (list target maptepl)))
		(get-cardinal-ref
			(list
				(list node nil nil nil)
				(list nil node nil nil)
				(list nil nil node nil)
				(list nil nil nil node)
			)
			dir
		)
	))
	
(define deep-room-hardlinks
  (list 
  	(list 0 1    (deep-room-mklink south 'p_lost_garrison 'm_deeptempl_passage))
	(list 1 1    (deep-room-mklink south nil 'm_deeptempl_wall))
  	(list -1 0   (deep-room-mklink east 'p_lost_garrison 'm_deeptempl_passage))
	(list 2 0    (deep-room-mklink west nil 'm_deeptempl_wall))
  	(list -1 -1  (deep-room-mklink east nil 'm_deeptempl_wall))
	(list 2 -1   (deep-room-mklink west nil 'm_deeptempl_wall))
	(list 0 -2   (deep-room-mklink north nil 'm_deeptempl_wall))
	(list 1 -2   (deep-room-mklink north nil 'm_deeptempl_wall))
  ))
	
(define (deep-room-gethardlink xloc yloc linksdata)
	(let (
		(matchinglink
			(filter (lambda (listentry)
				(and
					(equal? (car listentry) xloc)
					(equal? (cadr listentry) yloc)
				))
				linksdata)
			)
		)
		(if (equal? (length matchinglink) 0)
			(list nil nil nil nil)
			(caddr (car matchinglink)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; x y location tracking

;sets x and y location of new rooms based on current location and direction to new room
;also updates current -> prev -> normal status
(define (deep-room-setxy roomslist dir rxloc ryloc)
	(let* (
			(kplace (eval (get-cardinal-ref roomslist dir)))
			(roomdata (get-roomdata kplace))
			(offsets (get-cardinal-ref deep-room-offsets dir))
			(rxoff (+ rxloc (car offsets)))
			(ryoff (+ ryloc (cadr offsets)))
		)
		(if (not (deeps-roomdata-current roomdata))
			(deeps-roomdata-setxy roomdata rxoff ryoff))
		(deeps-roomdata-pushcurrent roomdata)
		))

;set xy stuff for all neighboring rooms, also sets current as current
(define (deep-room-init-neighbors kplace roomdata)
	(let* (
			(rooms (deeps-roomdata-rooms roomdata))
			(rxloc (deeps-roomdata-x roomdata))
			(ryloc (deeps-roomdata-y roomdata))
		)
		(map (lambda (dir)
			(deep-room-setxy rooms dir rxloc ryloc))
			(list north west east south))
		(deeps-roomdata-setcurrent roomdata #t)
		;debugging map
		(if #f
			(begin
				(kern-log-msg rxloc)
				(kern-log-msg ryloc))
		)
	))
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Room linking

; links in neighbors, or rooms from hardlink list, as appropriate
(define (deep-room-init-links kplace roomdata hardlinkdata)
	(let* (
			(rooms (deeps-roomdata-rooms roomdata))
			(rxloc (deeps-roomdata-x roomdata))
			(ryloc (deeps-roomdata-y roomdata))
			(linkinfo (deep-room-gethardlink rxloc ryloc hardlinkdata))
		)
		(map (lambda (dir)
			;roomlinktarget is hardlink target if it exists, else regular neighbor
			(let* (
				(thishardlink (get-cardinal-ref linkinfo dir))
				(hardlinktarget (if (null? thishardlink)
									nil
									(car thishardlink)))
				(roomlinktarget (if (null? hardlinktarget)
									(get-cardinal-ref rooms dir)
									hardlinktarget))
				)
				(kern-place-set-neighbor dir kplace (eval roomlinktarget))
			))
			(list north west east south))
	))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; map construction	
	
; do a single map blit
(define (deep-do-map-blit destmap srcmap blitstats)
	(let (
			(xloc (list-ref blitstats 0))
			(yloc (list-ref blitstats 1))
			(wid (list-ref blitstats 2))
			(hgt (list-ref blitstats 3))
		)	
		(kern-blit-map destmap xloc yloc
				(eval srcmap)
				xloc yloc wid hgt)
	))
	
; blit map for area and all sides. uses hard linked sides if given
(define (deep-room-blit-map kplace roomdata hardlinkdata blitstats)
	(let* (
			(rxloc (deeps-roomdata-x roomdata))
			(ryloc (deeps-roomdata-y roomdata))
			(linkinfo (deep-room-gethardlink rxloc ryloc hardlinkdata))
			(destmap (kern-place-map kplace))
			(rmapdata (list
				(list 0 1 deep-random-type-ns)
				(list 0 0 deep-random-type-ew)
				(list 1 0 deep-random-type-ew)
				(list 0 0 deep-random-type-ns)
				))
		)
		(deep-do-map-blit destmap
			(deep-get-template rxloc ryloc deep-random-type-area)
			(list-ref blitstats 4))
		(map (lambda (dir)
			;roomlinktarget is hardlink target if it exists, else regular neighbor
			(let* (
					(thishardlink (get-cardinal-ref linkinfo dir))
					(thisrmapdata (get-cardinal-ref rmapdata dir))
					(thisx (+ rxloc (car thisrmapdata)))
					(thisy (+ ryloc (cadr thisrmapdata)))
					(thisrtype (caddr thisrmapdata))
					(linkmap (if (null? thishardlink)
								(deep-get-template thisx thisy thisrtype)
								(cadr thishardlink)))
				)
				(deep-do-map-blit destmap linkmap
					(get-cardinal-ref blitstats dir))
			))
			(list north west east south))
	))
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; critters with alternative alignment

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; critter lists

(define deep-monster-types
	(list 
		(list 
			(list 100 'bat)
		)
		(list
			(list 100 'rat)
			(list 200 'bat)
		)
		(list
			(list 100 'giant-spider)
			(list 150 'queen-spider)
		)
		(list
			(list 100 'cave-goblin-slinger)
			(list 200 'cave-goblin-berserker)
			(list 250 'cave-goblin-priest)
		)
		(list
			(list 50 'cave-goblin-slinger-m)
			(list 100 'cave-goblin-berserker-m)
			(list 300 'troll-m)
			(list 350 'troll-geomancer-m)
		)
		(list
			(list 100 'green-slime)
			(list 200 'yellow-slime)
		)	
		(list
			(list 100 'zorn)
		)
		(list
			(list 20 'ghast)
			(list 200 'demon)
		)
		(list
			(list 50 'cave-goblin-slinger-m)
			(list 100 'cave-goblin-berserker-m)
			(list 150 'troll-m)
			(list 350 'gint-warrior-m)
			(list 400 'gint-mage-m)
		)
		(list
			(list 50 'cave-goblin-slinger-m)
			(list 100 'headless)
			(list 150 'cave-goblin-berserker-m)
			(list 200 'troll-m)
			(list 250 'gint-warrior-m)
			(list 350 'gazer)
		)
		(list
			(list 25 'cave-goblin-slinger-m)
			(list 50 'cave-goblin-berserker-m)
			(list 300 'dragon)
		)
		(list
			(list 20 'ghast)
			(list 70 'skeletal-warrior)
			(list 120 'skeletal-spear-thrower)
			(list 150 'craven-archer)
			(list 300 'death-knight)
			(list 350 'demon)
		)
		(list
			(list 100 'skeletal-warrior)
			(list 200 'skeletal-spear-thrower)
			(list 250 'lich)
		)
		(list 
			(list 100 'headless)
			(list 150 'skeletal-warrior)
			(list 200 'skeletal-spear-thrower)
			(list 220 'craven-archer)
			(list 250 'death-knight)
			(list 260 'demon)
			(list 330 'warlock)
		)
	)
)

(define deep-group-types
	(list
		(list 10 0 100 "1d4+3") ;bats
		(list 20 1 100 "1d4+3") ;rats
		(list 30 1 200 "1d6+4") ;bats n rats
		(list 40 5 100 "1d3+2") ;slime
		(list 50 2 100 "1d4+3") ;spiders
		(list 60 3 100 "1d3+1") ;goblins
		(list 70 2 110 "1d4+3") ;spiders 1q
		(list 80 13 100 "1d6+4") ;headless
		(list 90 2 150 "1d6+4") ;spiders +qs
		(list 100 5 200 "1d6+3") ;slime+
		(list 110 3 200 "1d6+4") ;goblin war
		(list 120 7 20 "1d6+1")  ;ghost
		(list 130 11 120 "1d6+4") ;skels
		(list 140 4 300 "1d6+3") ;trolls
		(list 150 6 100 "1d2")   ;zorn
		(list 160 8 350 "1d6+3") ;gint
		(list 170 7 22 "1d6+1")  ;ghost + d
		(list 180 11 300 "2d4+4") ;deathknights
		(list 190 11 310 "2d4+4") ;deathknights +d
		(list 200 3 210 "1d6+5") ;goblin war +p
		(list 210 7 200 "1d3")   ;demons
		(list 220 3 250 "2d4+4") ;goblin tribe
		(list 230 4 350 "1d6+5") ;trolls +m
		(list 240 8 400 "2d4+4") ;gint + m
		(list 250 10 300 "1d4")  ;dragon
		(list 260 10 80 "1d6+3") ;dragon + gob	
		(list 270 9 260 "1d6+3") ;gazer
		(list 280 13 330 "2d4+4") ;warlock		
		(list 290 12 205 "2d4+4") ;lich	
		(list 300 9 320 "2d4+4") ;gazers
	)
)

(define (deep-reduce-level level remaining)
	(max 1
		(- level (/ (* level (kern-dice-roll "1d100")) 50 (max remaining 2)))
		)
	)
	
(define (deep-get-monster-group quantity grouptype level typelev)
	(cons (mk-npc (car (get-numbered-elem grouptype typelev)) (ceiling (/ level 100)))
		(if (> quantity 0)
			(deep-get-monster-group (- quantity 1) grouptype
				(deep-reduce-level level quantity)
				(deep-reduce-level typelev quantity))
			nil)
	))
	
(define (deep-mk-monster-group group-types monster-types dice level)
	(let* (
			(grouptype (get-numbered-elem group-types (kern-dice-roll dice)))
			(grouplist (list-ref monster-types (car grouptype)))
		)
		(deep-get-monster-group (kern-dice-roll (caddr grouptype))
			grouplist level (cadr grouptype))
	))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; critters for random area

(define (deep-room-cleanout kplace)
	(map (lambda (obj)
		(if (equal? (kern-obj-get-type obj) t_roomdata)
			nil
			(begin 
				(kern-obj-remove obj))
		))
		(kern-place-get-objects kplace))
	)
	
(define (deep-room-addmonster kplace kchar)
	(kern-obj-put-at kchar 
		(random-loc-place-iter kplace 
			(lambda (loc)
				(and (passable? loc kchar)
					(not (is-bad-terrain-at? loc))
					(not (any-object-types-at? loc all-field-types))
					(not (occupied? loc))
				))
		15))
	)

; clears out old objects, and creates new ones
(define (deep-room-init-contents kplace roomdata)
	(let* (
			(rxloc (deeps-roomdata-x roomdata))
			(ryloc (deeps-roomdata-y roomdata))
			(distance (sqrt (+ (* rxloc rxloc) (* ryloc ryloc))))
		)
		(deep-room-cleanout kplace)
		(if (< (kern-dice-roll "1d100") 
				(min 75 (+ 25 (* 15 (sqrt distance)))))
			(begin 
			(map (lambda (monster)
				(begin 

					(deep-room-addmonster kplace monster)))
				(deep-mk-monster-group deep-group-types deep-monster-types 
					(string-append "1d" (number->string (min 300 (ceiling (* 120 (sqrt distance))))))
					(+ 400 (* 100 (sqrt distance)))))
					)
		)
	))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; handles

(define (deeps-room-handle-deeps kplace kplayer)
	(let* (
		(roomdata (get-roomdata kplace))
		)
		(deep-room-init-neighbors kplace roomdata)
		(deep-room-init-links kplace roomdata deep-room-hardlinks)
		(if (not (deeps-roomdata-prev roomdata))
			(begin
				(deep-room-blit-map kplace roomdata deep-room-hardlinks deep-room-blitstats)
				(deep-room-init-contents kplace roomdata))
		)
	))

(define (deeps-room-handle-garrison kplace kplayer)
	(let* (
		(roomdata (get-roomdata kplace))
		)
		(deep-room-init-neighbors kplace roomdata)
		(deep-room-init-links kplace roomdata deep-room-hardlinks)
	))	
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Plot type stuff

(mk-reusable-item 
 't_garrison_log "Garrison Log" s_lexicon 1
 (lambda (klexicon kuser)
   (kern-ui-page-text
   "Garrison Log"
   ""
   (string-append
	"The log details events at the garrison "
	"while it was still in operation. Early "
	"entries detail several attempts to "
	"clear the surrounding caverns.")
   ""
   (string-append
   "However, the paladins were "
   "unable to even determine how "
   "large the dungeon is, and "
   "eventually settled on a policy "
   "of containment by fortifying "
   "this bottleneck position.")
   )))

